2015-02-08 92 views
0

我有同樣的方式寫入多個字符串:刪除字符串

  • 名稱 -372198
  • 另一個 -9849204
  • 東西 -3439483

我想將字符串分成兩部分:連字符前的第一個字符和ot她在連字符後面。

我被Objective-c正則表達式卡住了!

回答

2

不知道你爲什麼使用正則表達式。出了什麼問題:

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

stringComponents陣列將舉行:{ @"Name", @"372198" }

代碼未經測試,但應該工作。

+0

我正在尋找這種方法,沒有找到。但似乎有一個!它的工作原理!謝謝。 – user3607093 2015-02-09 04:19:27

0

沒有必要在這種情況下,正則表達式。 這會做得很好

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

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