iphone - 字符串的子字符串

标签 iphone ios cocoa-touch

-(void)ClkRandomBtn:(id)sender
{

    NSMutableURLRequest *req = [[ NSMutableURLRequest alloc ] initWithURL: [ NSURL URLWithString:@"http://chinesepokerwithfriends.com/chinesepoker/rand_user.php?"]]; //POST set up. 
    NSMutableString *postBody = [NSMutableString stringWithFormat:@""];
    [req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
    [req setHTTPBody:[postBody dataUsingEncoding:NSUTF8StringEncoding]];
    [req setHTTPMethod:@"POST"];

    NSData *urlData;
    NSURLResponse *response;
    NSError *error;

    urlData = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];
    NSString *returnString = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];

    returnString= [returnString stringByReplacingOccurrencesOfString:@"\n"withString:@"" ];   
    NSLog(@"returnString=%@",returnString);


    randomplayer=[[NSMutableArray alloc]init];
    [randomplayer addObject:[returnString componentsSeparatedByString:@"/"]];


    NSMutableArray *Pinfo=[[NSMutableArray alloc]init];

    for (int i=0; i<[randomplayer count]; i++)
    {
        NSLog(@"%@",[randomplayer objectAtIndex:i]);

        NSString *strplayerinfo=[[NSString alloc]init];

        strplayerinfo=[randomplayer objectAtIndex:i];

        [Pinfo addObject:[strplayerinfo componentsSeparatedByString:@" "]];
    }

    for (int i=0; i<[Pinfo count]; i++)
    {
        NSLog(@" second array%@",[Pinfo objectAtIndex:i]);
    } 
}


这是我在dheeru 9411279057 dheeru@gmail.com/abc 9876543215 abc@gmail.com/中获取字符串的代码,我想将每个字符串拆分为单个字符串,但是此代码无法正常工作。如何使其运作?

最佳答案

更换

[randomplayer addObject:[returnString componentsSeparatedByString:@"/"]];




[randomplayer addObjectsFromArray:[returnString componentsSeparatedByString:@"/"]];


快速修复。



您可以用以下代码段替换方法的关闭代码段–

NSString * trimmedString = [returnString stringByTrimmingCharactersInSet:[NSCharacterSet  characterSetWithCharactersInString:@"/"];
NSArray * chunks = [trimmedString componentsSeparatedByString:@"/"];

NSMutableArray *Pinfo=[[NSMutableArray alloc]init];

for (int i = 0; i < [chunks count]; i++)
{
    NSString * playerInfo = [chunks objectAtIndex:i];

    [Pinfo addObject:[playerInfo componentsSeparatedByString:@" "]];
}

for (int i = 0; i < [Pinfo count]; i++)
{
    NSArray * info = [Pinfo objectAtIndex:i];

    NSLog(@"Player %d", i);
    NSLog(@"Name: %@", [info objectAtIndex:0]);
    NSLog(@"Phone: %@", [info objectAtIndex:1]);
    NSLog(@"Email: %@", [info objectAtIndex:2]);
} 

关于iphone - 字符串的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6464546/

相关文章:

iphone - 在 UIButton 中开始单击,但在主视图中使用 touchesEnded

iPhone:UITabBarController 内的半透明导航栏

iphone - 在 Windows 上模拟 iPad、iPhone

ios - 在营业时间的基础上,如何确定一家公司是否营业(Swift-iOS)

ios - Swift 2.2字符串文字选择器

iphone - 如何创建可变大小的 UITableViewCell?

iOS 多模块应用

ios - 仪器,iPhone应用程序分配增加

ios - Swift:executeFetchRequest:无法转换“任何对象”类型的值

iphone - 从NSString删除开头和结尾的换行符