Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何从变异字符串创建子字符串_Iphone_Ios_String_Nsarray_Substring - Fatal编程技术网

Iphone 如何从变异字符串创建子字符串

Iphone 如何从变异字符串创建子字符串,iphone,ios,string,nsarray,substring,Iphone,Ios,String,Nsarray,Substring,我是iphone开发的新手,我正在尝试从NSString值创建一个子字符串,该值总是会变化的。基本上,我正在尝试对字符串的最后一部分进行子串,这将有所不同。字符串的格式为“March 15 PM Metals Commentation:John Jones”。我想要字符串的姓氏部分,例如,“Jones”而不是“John Jones”。我尝试的两种方法要么给我异常,要么不返回值。非常感谢您的帮助 我的代码 //This approach throw index out of bounds exce

我是iphone开发的新手,我正在尝试从NSString值创建一个子字符串,该值总是会变化的。基本上,我正在尝试对字符串的最后一部分进行子串,这将有所不同。字符串的格式为“March 15 PM Metals Commentation:John Jones”。我想要字符串的姓氏部分,例如,“Jones”而不是“John Jones”。我尝试的两种方法要么给我异常,要么不返回值。非常感谢您的帮助

我的代码

//This approach throw index out of bounds exception
NSString * storyLink2 = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];
NSRange namestart = [storyLink2 rangeOfString:@" " options:NSBackwardsSearch];
NSString *name = [[[stories objectAtIndex: storyIndex] objectForKey: @"title"] substringWithRange:NSMakeRange(namestart.location +1, name.length - namestart.location+1)];

//This approach return nothing
NSString *name = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];
NSArray *thingitems = [name componentsSeparatedByString:@" "];
name = [thingitems lastObject];
编辑

如果您的字符串是
“March 15 PM Metals Commentation:John Jones”
,并且您总是在冒号后面的最后一部分输入姓名:“”,则:

NSString *str=@"March 15 PM Metals Commentary: John Jones";
//use storyLink2 in place of str
NSString *name=[str componentsSeparatedByString:@":"][1];
编辑:

如果你只想知道姓氏:那么

NSString *str=@"March 15 PM Metals Commentary: John Jones";
//use storyLink2 in place of str
NSArray *fullname=[str componentsSeparatedByString:@" "];
NSArray *lastName=[fullname lastObject];

如果您只需要
字符串中的最后一个单词,请尝试
[[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]lastObject]

我已经测试过了,没有错误

NSString *string = @"March 15 PM Metals Commentary: John Jones";
NSString *lastName = [[string componentsSeparatedByString:@" "] lastObject];
NSLog(@"%@",lastName);
所以我想你的可能是这样的

NSString *name = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];
NSString *lastName=[[name componentsSeparatedByString:@" "] lastObject];
NSLog(@"myname: %@", lastName);

如果名字总是在
后面,那么将
上的字符串拆分,并在后面取一部分。我只想要姓氏,而不是名字谢谢您的回复。我只想要Jones,而不是John Jones我现在用这种方法得到一个无法识别的选择器错误storyLink2包含完整的strng,如“3月15日下午金属评论:John Jones”?是的,它在我的日志中显示“3月15日下午金属评论:John Jones”?哪一行显示unrecoz选择器?哪个编译器/xcode版本?
NSString *string = @"March 15 PM Metals Commentary: John Jones";
NSString *lastName = [[string componentsSeparatedByString:@" "] lastObject];
NSLog(@"%@",lastName);
NSString *name = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];
NSString *lastName=[[name componentsSeparatedByString:@" "] lastObject];
NSLog(@"myname: %@", lastName);