Swift : substringWithRange() function example
A quick tutorial as add on from previous tutorial on how to get sub-string with rangeOfString() function. Another way to get sub string in Swift is to use the substringWithRange()
function.
For example :
var str = "Hello World!"
str.substringWithRange(Range<String.Index>(start: str.startIndex, end: str.endIndex))
Output :
"Hello World!"
and if you want to start from position 3 and position -1 from end. Change the start
and end
position with advance()
function.
var str = "Hello World!"
str.substringWithRange(Range<String.Index>(start: advance(str.startIndex, 3), end: advance(str.endIndex, -1)))
Output :
"lo World"
See also : Swift : Get substring with rangeOfString() function example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+18.8k Golang : Read directory content with os.Open
+5k Linux/MacOSX : Search for files by filename and extension with find command
+5k Golang : Function as an argument type example
+14.9k Golang : convert string or integer to big.Int type
+12.1k Golang : Image to ASCII art example
+8.9k Golang : Get current, epoch time and display by year, month and day
+4.9k Golang : Launching your executable inside a console under Linux
+28.7k Get client IP Address in Go
+9.4k Golang : Underscore string example
+6.9k Golang : Gomobile init produce "iphoneos" cannot be located error
+6.3k Golang : Word limiter example
+8.8k Golang : Function wrapper that takes arguments and return result example