IOS NSString在“#”之前获取字符

时间:2014-08-17 13:25:24

标签: ios objective-c nsstring

例如我有这样的字符串:

NSString * one = B3#这是第一个字符串 NSString * two = 1#这是第二个字符串

我如何才能获得“B3”和“1”字符(使用目标C)

谢谢..

2 个答案:

答案 0 :(得分:3)

原来这是一种方法:

NSRange range = [one rangeOfString:@"#" options:NSBackwardsSearch];
NSString *newString = [one substringToIndex:range.location];

感谢所有答案。

答案 1 :(得分:0)

NSString* one = @"B3#";
NSString* two = @"1#";

NSString* result = [one stringByReplacingOccurrencesOfString:@"#" withString:@""];
NSString* result_2 = [two stringByReplacingOccurrencesOfString:@"#" withString:@""];

//if you need to marge
NSString* tot = [NSString stringWithFormat:@"%@%@",result,result_2];