我有一个这样的字符串:@"10/04/2011"
我只想将“10”保存在另一个字符串中。我该怎么做?
最佳答案
NSArray* foo = [@"10/04/2011" componentsSeparatedByString: @"/"];
NSString* firstBit = [foo objectAtIndex: 0];
2018 年 7 月 3 日更新:
既然问题已经获得了 Swift 标签,我应该添加 Swift 的方式来执行此操作。就这么简单:
let substrings = "10/04/2011".split(separator: "/")
let firstBit = substrings[0]
虽然请注意它为您提供了一个 Substring
数组。如果您需要将这些转换回普通字符串,请使用 map
let strings = "10/04/2011".split(separator: "/").map{ String($0) }
let firstBit = strings[0]
或
let firstBit = String(substrings[0])
https://stackoverflow.com/questions/5806852/