Swift:如何获得某个字符之前的string?

如何快速获取特定字符之前的string? 下面的代码是我在Objective C中做的,但似乎无法在Swift中执行相同的任务。 有关如何实现这一目标的任何提示或build议? rangeOfString在swift中似乎根本不起作用(尽pipeSwift一直在为我提供帮助)。

 NSRange range = [time rangeOfString:@" "]; NSString *startDate = [time substringToIndex:range.location]; 

正如你从上面的代码中可以看到的,我可以在Objective C中的空格字符之前获取string。

编辑:如果我尝试这样的事情

  var string = "hello Swift" var range : NSRange = string.rangeOfString("Swift") 

我得到以下错误。

无法将expression式的types'NSString'转换为types'(String,options:NSStringCompareOptions,range:Range?,locale:NSLocale?)'

不知道我做错了什么,或如何正确解决它。

使用componentsSeparatedByString()如下所示:

 var delimiter = " " var newstr = "token0 token1 token2 token3" var token = newstr.componentsSeparatedByString(delimiter) print (token[0]) 

或者使用您的具体情况:

 var delimiter = " token1" var newstr = "token0 token1 token2 token3" var token = newstr.componentsSeparatedByString(delimiter) print (token[0]) 

Swift 3编辑:

使用Swift 3,上面的方法不再有效,请使用下面的代码:

 var token = newstr.components(separatedBy: delimiter) 

如果这个编辑帮助你,请注意这个答案 。

你可以用String类提供的rangeOfString()来做同样的rangeOfString()

 let string = "Hello Swift" if let range = string.rangeOfString("Swift") { let firstPart = string[string.startIndex..<range.startIndex] print(firstPart) // print Hello } 

你也可以用你的方法substringToIndex()

 let string = "Hello Swift" if let range = string.rangeOfString("Swift") { firstPart = string.substringToIndex(range.startIndex) print(firstPart) // print Hello } 

Swift 3更新:

 let string = "Hello Swift" if let range = string.range(of: "Swift") { let firstPart = string[string.startIndex..<range.lowerBound] print(firstPart) // print Hello } 

希望这可以帮到你 ;)

如果你想要一个不牵涉到基础的解决scheme,你可以用find和slicing来完成:

 let str = "Hello, I must be going." if let comma = find(str, ",") { let substr = str[str.startIndex..<comma] // substr will be "Hello" } 

如果在没有find这样的字符的情况下显式地需要一个空string,则可以使用nil-coalescing运算符:

 let str = "no comma" let comma = find(str, ",") ?? str.startIndex let substr = str[str.startIndex..<comma] // substr will be "" 

请注意,与componentsSeparatedByString技术不同,这不需要创build数组,只需要扫描到第一次出现的字符,而不是将整个string分解为以字符分隔的数组。

继续Syed Tariq的回答:如果您只想在分隔符之前的string(否则,您收到数组[String]):

 var token = newstr.components(separatedBy: delimiter).first 

要将string转换为string,直到第一次出现指定的string,可以像这样扩展string:

 extension String { mutating func until(_ string: String) { var components = self.components(separatedBy: string) self = components[0] } } 

这可以这样调用:

 var foo = "Hello Swift" foo.until(" Swift") // foo is now "Hello" 

你可以使用rangeOfString ,但是它返回一个Range<String.Index>types,而不是NSRange

 let string = "hello Swift" if let range = string.rangeOfString("Swift") { print(string.substringToIndex(range.startIndex)) } 

我的2美分:-)使用Swift 3.0,类似于PHP的strstr

 extension String { func strstr(needle: String, beforeNeedle: Bool = false) -> String? { guard let range = self.range(of: needle) else { return nil } if beforeNeedle { return self.substring(to: range.lowerBound) } return self.substring(from: range.upperBound) } } 

Usage1

 "Hello, World!".strstr(needle: ",", beforeNeedle: true) // returns Hello 

Usage2

 "Hello, World!".strstr(needle: " ") // returns World! 
 let string = "Hello-world" if let range = string.range(of: "-") { let firstPart = string[(string.startIndex)..<range.lowerBound] print(firstPart) } 

输出是:你好

下面是一个完整的组合

 let string = "This a string split using * and this is left." if let range = string.range(of: "*") { let lastPartIncludingDelimiter = string.substring(from: range.lowerBound) print(lastPartIncludingDelimiter) // print * and this is left. let lastPartExcludingDelimiter = string.substring(from: range.upperBound) print(lastPartExcludingDelimiter) // print and this is left. let firstPartIncludingDelimiter = string.substring(to: range.upperBound) print(firstPartIncludingDelimiter) // print This a string split using * let firstPartExcludingDelimiter = string.substring(to: range.lowerBound) print(firstPartExcludingDelimiter) // print This a string split using }