Linked Questions
26 questions linked to/from How do you use String.substringWithRange? (or, how do Ranges work in Swift?)
131 votes
2 answers
93k views
How to get last 4 characters of a string? [duplicate]
I need to get the last 4 letters of a string. How can I do that? The length of the string varies. Example: var a = "StackOverFlow" var last4 = a.lastFour // That's what I want to do print(...
-2 votes
1 answer
752 views
How to get substring of String in Swift? [duplicate]
If I want to get a value from the NSString "😃hello World😃", what should I use? The return value I want is "hello World". The smileys can be any string. so i need some regexp to this.
0 votes
1 answer
200 views
How do i get a subString? [duplicate]
Could someone pls tell me how i get a subString from a String? I tried everything with substring and rangeOfString methods but can't solve it... My text goes like this: var text = "Suuuuper, {1} has ...
0 votes
1 answer
129 views
Creating A Substring | Swift 4 [duplicate]
How do I substring a string in swift 4. I would like to convert: "Hello, World!" Into: "Hello"
584 votes
51 answers
329k views
How can I create a UIColor from a hex string?
How can I create a UIColor from a hexadecimal string format, such as #00FF00?
436 votes
16 answers
232k views
Does Swift have a trim method on String?
Does Swift have a trim() method on String? For example: let result = " abc ".trim() // result == "abc"
222 votes
33 answers
282k views
Finding index of character in Swift String
It's time to admit defeat... In Objective-C, I could use something like: NSString* str = @"abcdefghi"; [str rangeOfString:@"c"].location; // 2 In Swift, I see something similar: var str = "...
121 votes
23 answers
164k views
Swift: How to get substring from start to last index of character
I want to learn the best/simplest way to turn a string into another string but with only a subset, starting at the beginning and going to the last index of a character. For example, convert "www....
117 votes
2 answers
82k views
How to check what a String starts with (prefix) or ends with (suffix) in Swift
I'm trying to test if a Swift string starts or ends with a certain value. These methods do not exist: var str = "Hello, playground" str.startsWith("Hello") // error str.endsWith("ground") // error I ...
67 votes
1 answer
60k views
How to use substringToIndex in Swift? [duplicate]
I get compiler error at this line: UIDevice.currentDevice().identifierForVendor.UUIDString.substringToIndex(8) Type 'String.Index' does not conform to protocol 'IntegerLiteralConvertible' My ...
36 votes
13 answers
23k views
How to split a string into substrings of equal length
So split("There are fourty-eight characters in this string", 20) should return ["There are fourty-eig", "ht characters in thi","s string"] If I make currentIndex = string.startIndex and then try to ...
24 votes
13 answers
33k views
Using stringByReplacingCharactersInRange in Swift
I'm trying to use UITextFieldDelegate in Swift/Xcode6 and I'm struggling with the way I'm supposed to use stringByReplacingCharactersInRange. The compiler error is 'Cannot convert the expression's ...
26 votes
3 answers
12k views
Type 'String.Index' does not conform protocol 'IntegerLiteralConvertible'
With Beta 3 all worked fine, now I get a strange error, and I have no clue how to fix it. Tried all the solutions for similiar problems. Here is my code: if !name.isEmpty { var splitted: [...
15 votes
1 answer
16k views
Understanding the removeRange(_:) documentation
To remove a substring at a specified range, use the removeRange(_:) method: 1 let range = advance(welcome.endIndex, -6)..<welcome.endIndex 2 welcome.removeRange(range) 3 println(welcome) 4 // ...
10 votes
2 answers
5k views
How to efficiently find CGRects for visible words in UITextView?
My goal is to mark all visible misspelled words in an UITextView. The inefficient algorithm is to use the spell checker to find all ranges of misspelled words in the text, convert them to ...