删除连字符后的字符串

Remove string after the hyphen

我有多个以相同方式编写的字符串:

我想将字符串分成两部分:第一部分在连字符之前,另一部分在连字符之后。

我受困于 Objective-c 正则表达式!

如果您确定只有一个 -,您可能想看看

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/#//apple_ref/occ/instm/NSString/rangeOfString:options:

在这种情况下不需要正则表达式。 这样就可以了

NSArray *stringParts=[yourString componentsSeparatedByString:@"-"];

NSString *part1=stringParts[0]; 
// has the part before the hyphen
NSString *part2=stringParts[1];
// has the part after the hyphen

不确定为什么要使用正则表达式。有什么问题:

NSString *theString = @"Name-372198";
NSArray *stringComponents = [theString componentsSeparatedByString:@"-"];

stringComponents 数组将包含:{ @"Name", @"372198" }

代码未经测试但应该可以工作。