Usage of String.range in Swift 3.0
In swift 3.0 rangeOfString
syntax changed like this.
let us = "http://example.com"
let range = us.range(of:"(?<=://)[^.]+(?=.com)", options:.regularExpression)
if range != nil {
let found = us.substring(with: range!)
print("found: \(found)") // found: example
}
How do you use String.substringWithRange? (or, how do Ranges work in Swift?)
You can use the substringWithRange method. It takes a start and end String.Index.
var str = "Hello, playground"
str.substringWithRange(Range<String.Index>(start: str.startIndex, end: str.endIndex)) //"Hello, playground"
To change the start and end index, use advancedBy(n).
var str = "Hello, playground"
str.substringWithRange(Range<String.Index>(start: str.startIndex.advancedBy(2), end: str.endIndex.advancedBy(-1))) //"llo, playgroun"
You can also still use the NSString method with NSRange, but you have to make sure you are using an NSString like this:
let myNSString = str as NSString
myNSString.substringWithRange(NSRange(location: 0, length: 3))
Note: as JanX2 mentioned, this second method is not safe with unicode strings.
How to get range of substring in swift3?
let string = "Please Click Here"
if let range = string.range(of: "Click") {
print(range)
}
How does String substring work in Swift
All of the following examples use
var str = "Hello, playground"
Swift 4
Strings got a pretty big overhaul in Swift 4. When you get some substring from a String now, you get a Substring
type back rather than a String
. Why is this? Strings are value types in Swift. That means if you use one String to make a new one, then it has to be copied over. This is good for stability (no one else is going to change it without your knowledge) but bad for efficiency.
A Substring, on the other hand, is a reference back to the original String from which it came. Here is an image from the documentation illustrating that.
No copying is needed so it is much more efficient to use. However, imagine you got a ten character Substring from a million character String. Because the Substring is referencing the String, the system would have to hold on to the entire String for as long as the Substring is around. Thus, whenever you are done manipulating your Substring, convert it to a String.
let myString = String(mySubstring)
This will copy just the substring over and the memory holding old String can be reclaimed. Substrings (as a type) are meant to be short lived.
Another big improvement in Swift 4 is that Strings are Collections (again). That means that whatever you can do to a Collection, you can do to a String (use subscripts, iterate over the characters, filter, etc).
The following examples show how to get a substring in Swift.
Getting substrings
You can get a substring from a string by using subscripts or a number of other methods (for example, prefix
, suffix
, split
). You still need to use String.Index
and not an Int
index for the range, though. (See my other answer if you need help with that.)
Beginning of a string
You can use a subscript (note the Swift 4 one-sided range):
let index = str.index(str.startIndex, offsetBy: 5)
let mySubstring = str[..<index] // Hello
or prefix
:
let index = str.index(str.startIndex, offsetBy: 5)
let mySubstring = str.prefix(upTo: index) // Hello
or even easier:
let mySubstring = str.prefix(5) // Hello
End of a string
Using subscripts:
let index = str.index(str.endIndex, offsetBy: -10)
let mySubstring = str[index...] // playground
or suffix
:
let index = str.index(str.endIndex, offsetBy: -10)
let mySubstring = str.suffix(from: index) // playground
or even easier:
let mySubstring = str.suffix(10) // playground
Note that when using the suffix(from: index)
I had to count back from the end by using -10
. That is not necessary when just using suffix(x)
, which just takes the last x
characters of a String.
Range in a string
Again we simply use subscripts here.
let start = str.index(str.startIndex, offsetBy: 7)
let end = str.index(str.endIndex, offsetBy: -6)
let range = start..<end
let mySubstring = str[range] // play
Converting Substring
to String
Don't forget, when you are ready to save your substring, you should convert it to a String
so that the old string's memory can be cleaned up.
let myString = String(mySubstring)
Using an Int
index extension?
I'm hesitant to use an Int
based index extension after reading the article Strings in Swift 3 by Airspeed Velocity and Ole Begemann. Although in Swift 4, Strings are collections, the Swift team purposely hasn't used Int
indexes. It is still String.Index
. This has to do with Swift Characters being composed of varying numbers of Unicode codepoints. The actual index has to be uniquely calculated for every string.
I have to say, I hope the Swift team finds a way to abstract away String.Index
in the future. But until then, I am choosing to use their API. It helps me to remember that String manipulations are not just simple Int
index lookups.
Swift 4 string range of a certain character
The String.range
function can be provided extra options, so you can pass it the String.CompareOptions.backwards
parameters to get the last instance.
func deleteInnerString(with string: String, from firstCharacter: String, to secondCharacter: String) -> String {
guard let leftIndex = (string.range(of: firstCharacter)?.lowerBound),
let rightIndex = string.range(of: secondCharacter, options: .backwards)?.lowerBound else {
return string
}
let remainingString = String(string.prefix(upTo: leftIndex) + string.suffix(from: string.index(after: rightIndex)))
return remainingString
}
How to create range in Swift?
Updated for Swift 4
Swift ranges are more complex than NSRange
, and they didn't get any easier in Swift 3. If you want to try to understand the reasoning behind some of this complexity, read this and this. I'll just show you how to create them and when you might use them.
Closed Ranges: a...b
This range operator creates a Swift range which includes both element a
and element b
, even if b
is the maximum possible value for a type (like Int.max
). There are two different types of closed ranges: ClosedRange
and CountableClosedRange
.
1. ClosedRange
The elements of all ranges in Swift are comparable (ie, they conform to the Comparable protocol). That allows you to access the elements in the range from a collection. Here is an example:
let myRange: ClosedRange = 1...3
let myArray = ["a", "b", "c", "d", "e"]
myArray[myRange] // ["b", "c", "d"]
However, a ClosedRange
is not countable (ie, it does not conform to the Sequence protocol). That means you can't iterate over the elements with a for
loop. For that you need the CountableClosedRange
.
2. CountableClosedRange
This is similar to the last one except now the range can also be iterated over.
let myRange: CountableClosedRange = 1...3
let myArray = ["a", "b", "c", "d", "e"]
myArray[myRange] // ["b", "c", "d"]
for index in myRange {
print(myArray[index])
}
Half-Open Ranges: a..<b
This range operator includes element a
but not element b
. Like above, there are two different types of half-open ranges: Range
and CountableRange
.
1. Range
As with ClosedRange
, you can access the elements of a collection with a Range
. Example:
let myRange: Range = 1..<3
let myArray = ["a", "b", "c", "d", "e"]
myArray[myRange] // ["b", "c"]
Again, though, you cannot iterate over a Range
because it is only comparable, not stridable.
2. CountableRange
A CountableRange
allows iteration.
let myRange: CountableRange = 1..<3
let myArray = ["a", "b", "c", "d", "e"]
myArray[myRange] // ["b", "c"]
for index in myRange {
print(myArray[index])
}
NSRange
You can (must) still use NSRange
at times in Swift (when making attributed strings, for example), so it is helpful to know how to make one.
let myNSRange = NSRange(location: 3, length: 2)
Note that this is location and length, not start index and end index. The example here is similar in meaning to the Swift range 3..<5
. However, since the types are different, they are not interchangeable.
Ranges with Strings
The ...
and ..<
range operators are a shorthand way of creating ranges. For example:
let myRange = 1..<3
The long hand way to create the same range would be
let myRange = CountableRange<Int>(uncheckedBounds: (lower: 1, upper: 3)) // 1..<3
You can see that the index type here is Int
. That doesn't work for String
, though, because Strings are made of Characters and not all characters are the same size. (Read this for more info.) An emoji like , for example, takes more space than the letter "b".
Problem with NSRange
Try experimenting with NSRange
and an NSString
with emoji and you'll see what I mean. Headache.
let myNSRange = NSRange(location: 1, length: 3)
let myNSString: NSString = "abcde"
myNSString.substring(with: myNSRange) // "bcd"
let myNSString2: NSString = "acde"
myNSString2.substring(with: myNSRange) // "c" Where is the "d"!?
The smiley face takes two UTF-16 code units to store, so it gives the unexpected result of not including the "d".
Swift Solution
Because of this, with Swift Strings you use Range<String.Index>
, not Range<Int>
. The String Index is calculated based on a particular string so that it knows if there are any emoji or extended grapheme clusters.
Example
var myString = "abcde"
let start = myString.index(myString.startIndex, offsetBy: 1)
let end = myString.index(myString.startIndex, offsetBy: 4)
let myRange = start..<end
myString[myRange] // "bcd"
myString = "acde"
let start2 = myString.index(myString.startIndex, offsetBy: 1)
let end2 = myString.index(myString.startIndex, offsetBy: 4)
let myRange2 = start2..<end2
myString[myRange2] // "cd"
One-sided Ranges: a...
and ...b
and ..<b
In Swift 4 things were simplified a bit. Whenever the starting or ending point of a range can be inferred, you can leave it off.
Int
You can use one-sided integer ranges to iterate over collections. Here are some examples from the documentation.
// iterate from index 2 to the end of the array
for name in names[2...] {
print(name)
}
// iterate from the beginning of the array to index 2
for name in names[...2] {
print(name)
}
// iterate from the beginning of the array up to but not including index 2
for name in names[..<2] {
print(name)
}
// the range from negative infinity to 5. You can't iterate forward
// over this because the starting point in unknown.
let range = ...5
range.contains(7) // false
range.contains(4) // true
range.contains(-1) // true
// You can iterate over this but it will be an infinate loop
// so you have to break out at some point.
let range = 5...
String
This also works with String ranges. If you are making a range with str.startIndex
or str.endIndex
at one end, you can leave it off. The compiler will infer it.
Given
var str = "Hello, playground"
let index = str.index(str.startIndex, offsetBy: 5)
let myRange = ..<index // Hello
You can go from the index to str.endIndex by using ...
var str = "Hello, playground"
let index = str.index(str.endIndex, offsetBy: -10)
let myRange = index... // playground
See also:
- How does String.Index work in Swift
- How does String substring work in Swift
Notes
- You can't use a range you created with one string on a different string.
- As you can see, String ranges are a pain in Swift, but they do make it possibly to deal better with emoji and other Unicode scalars.
Further Study
- String Range examples
- Range Operator documentation
- Strings and Characters documentation
Swift 3: How to do string ranges?
Simple answer: the fundamental thing you are missing is that a closed range is now different from a range. So, change startRange...endRange
to startRange..<endRange
.
In more detail, here's an abbreviated version of your code (without the maxChar
part):
let val = "some long string";
var startRange = val.startIndex;
var endRange = val.endIndex;
let index = val.range(
of: " ", options: .backwards, range: startRange..<endRange)?.lowerBound
// 9
Now you can use that as a basis to restore your actual desired functionality.
However, if all you want to do is split the string, then reinventing the wheel is kind of silly:
let arr = "hey ho ha".characters.split(separator:" ").map{String($0)}
arr // ["hey", "ho", "ha"]
Swift: How to check if a range is valid for a given string
In Swift 3, this is easy: just get the string's character range and call contains
to see if it contains your arbitrary range.
Edit: In Swift 4, a range no longer "contains" a range. A Swift 4.2 solution might look like this:
let string = // some string
let range = // some range of String.Index
let ok = range.clamped(to: string.startIndex..<string.endIndex) == range
If ok
is true, it is safe to apply range
to string
.
NSRange from Swift Range?
Swift String
ranges and NSString
ranges are not "compatible".
For example, an emoji like counts as one Swift character, but as two NSString
characters (a so-called UTF-16 surrogate pair).
Therefore your suggested solution will produce unexpected results if the string
contains such characters. Example:
let text = "Long paragraph saying!"
let textRange = text.startIndex..<text.endIndex
let attributedString = NSMutableAttributedString(string: text)
text.enumerateSubstringsInRange(textRange, options: NSStringEnumerationOptions.ByWords, { (substring, substringRange, enclosingRange, stop) -> () in
let start = distance(text.startIndex, substringRange.startIndex)
let length = distance(substringRange.startIndex, substringRange.endIndex)
let range = NSMakeRange(start, length)
if (substring == "saying") {
attributedString.addAttribute(NSForegroundColorAttributeName, value: NSColor.redColor(), range: range)
}
})
println(attributedString)
Output:
Long paragra{
}ph say{
NSColor = "NSCalibratedRGBColorSpace 1 0 0 1";
}ing!{
}
As you see, "ph say" has been marked with the attribute, not "saying".
Since NS(Mutable)AttributedString
ultimately requires an NSString
and an NSRange
, it is actually
better to convert the given string to NSString
first. Then the substringRange
is an NSRange
and you don't have to convert the ranges anymore:
let text = "Long paragraph saying!"
let nsText = text as NSString
let textRange = NSMakeRange(0, nsText.length)
let attributedString = NSMutableAttributedString(string: nsText)
nsText.enumerateSubstringsInRange(textRange, options: NSStringEnumerationOptions.ByWords, { (substring, substringRange, enclosingRange, stop) -> () in
if (substring == "saying") {
attributedString.addAttribute(NSForegroundColorAttributeName, value: NSColor.redColor(), range: substringRange)
}
})
println(attributedString)
Output:
Long paragraph {
}saying{
NSColor = "NSCalibratedRGBColorSpace 1 0 0 1";
}!{
}
Update for Swift 2:
let text = "Long paragraph saying!"
let nsText = text as NSString
let textRange = NSMakeRange(0, nsText.length)
let attributedString = NSMutableAttributedString(string: text)
nsText.enumerateSubstringsInRange(textRange, options: .ByWords, usingBlock: {
(substring, substringRange, _, _) in
if (substring == "saying") {
attributedString.addAttribute(NSForegroundColorAttributeName, value: NSColor.redColor(), range: substringRange)
}
})
print(attributedString)
Update for Swift 3:
let text = "Long paragraph saying!"
let nsText = text as NSString
let textRange = NSMakeRange(0, nsText.length)
let attributedString = NSMutableAttributedString(string: text)
nsText.enumerateSubstrings(in: textRange, options: .byWords, using: {
(substring, substringRange, _, _) in
if (substring == "saying") {
attributedString.addAttribute(NSForegroundColorAttributeName, value: NSColor.red, range: substringRange)
}
})
print(attributedString)
Update for Swift 4:
As of Swift 4 (Xcode 9), the Swift standard library
provides method to convert between Range<String.Index>
and NSRange
.
Converting to NSString
is no longer necessary:
let text = "Long paragraph saying!"
let attributedString = NSMutableAttributedString(string: text)
text.enumerateSubstrings(in: text.startIndex..<text.endIndex, options: .byWords) {
(substring, substringRange, _, _) in
if substring == "saying" {
attributedString.addAttribute(.foregroundColor, value: NSColor.red,
range: NSRange(substringRange, in: text))
}
}
print(attributedString)
Here substringRange
is a Range<String.Index>
, and that is converted to the
corresponding NSRange
with
NSRange(substringRange, in: text)
Related Topics
Realm Accessed from Incorrect Thread
Google Maps Sdk Not Displaying Properly in Uiview
How to Change Color of Text Strings Inside Uitextview in Swift3
Can an iOS App Switch the Device to Silent Mode
Alamofire 5: Value of Type 'Result<Data, Aferror>' Has No Member 'Value'
iOS 10: How to Show Incoming Voip Call Notification When App Is in Background
Cgcolorgetcomponents() Not Returning the Correct Values for Black and for White
iOS Simulator Is Not Launching
How to Create/Extract an Array of Views Using @Viewbuilder in Swiftui
Horizontally Scroll All Rows of Uicollectionview Together
How to Use Mkpolylineview in Swift
Native Zlib Inflate/Deflate for Swift3 on iOS
Uitextview Text Selection and Highlight Jumping in iOS 8
Xcode No Valid Compiled Storyboard at Path
Set<Nsobject>' Does Not Have a Member Named 'Anyobject." - Xcode 6.3