Home:ALL Converter>Swift : How to get the string before a certain character?

Swift : How to get the string before a certain character?

Ask Time:2015-04-03T04:18:40         Author:Danger Veger

Json Formatter

How do I get the string before a certain character in swift? The code below is how I did it in Objective C, but can't seem to perform the same task in Swift. Any tips or suggestions on how to achieve this? rangeOfString seems to not work at all in swift (although Swift has been acting up for me again).

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

As you can see from the code above I am able to get the string before the space character in Objective C.

Edit : If I try something like this

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

I get the following error.

Cannot convert the expression's type 'NSString' to type '(String, options: NSStringCompareOptions, range: Range?, locale: NSLocale?)'

Not sure what I did wrong exactly or how to resolve it correctly.

Author:Danger Veger,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/29421726/swift-how-to-get-the-string-before-a-certain-character
Syed Tariq :

Use componentsSeparatedByString() as shown below:\n\nvar delimiter = \" \"\nvar newstr = \"token0 token1 token2 token3\"\nvar token = newstr.components(separatedBy: delimiter)\nprint (token[0])\n\n\nOr to use your specific case:\n\nvar delimiter = \" token1\"\nvar newstr = \"token0 token1 token2 token3\"\nvar token = newstr.components(separatedBy: delimiter)\nprint (token[0])\n",
2015-04-02T20:31:10
Kevin Machado :

You can do the same with rangeOfString() provided by String class\n\nlet string = \"Hello Swift\"\nif let range = string.rangeOfString(\"Swift\") {\n let firstPart = string[string.startIndex..<range.startIndex]\n print(firstPart) // print Hello\n}\n\n\nYou can also achieve it with your method substringToIndex()\n\nlet string = \"Hello Swift\"\nif let range = string.rangeOfString(\"Swift\") {\n firstPart = string.substringToIndex(range.startIndex)\n print(firstPart) // print Hello\n}\n\n\nSwift 3 UPDATE:\n\nlet string = \"Hello Swift\"\nif let range = string.range(of: \"Swift\") {\n let firstPart = string[string.startIndex..<range.lowerBound]\n print(firstPart) // print Hello\n}\n\n\nHope this can help you ;)",
2015-04-02T20:48:16
Tim Newton :

Following up on Syed Tariq's answer: If you only want the string before the delimiter (otherwise, you receive an array [String]):\n\nvar token = newstr.components(separatedBy: delimiter).first\n",
2017-04-05T20:01:26
trndjc :

Swift 5 extension\nextension String {\n func before(first delimiter: Character) -> String {\n if let index = firstIndex(of: delimiter) {\n let before = prefix(upTo: index)\n return String(before)\n }\n return ""\n }\n \n func after(first delimiter: Character) -> String {\n if let index = firstIndex(of: delimiter) {\n let after = suffix(from: index).dropFirst()\n return String(after)\n }\n return ""\n }\n}\n\nlet str = "n1:lolz"\nstr.before(first: ":") // n1\nstr.after(first: ":") // lolz\nstr.after(first: "z") // empty string\nstr.before(first: "n") // empty string\nstr.before(first: "g") // empty string\n\nI think it makes as much sense to return an optional String for preference (returning nil if there isn't anything before or after the delimiter).",
2020-09-28T19:12:06
yy