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
+4k Golang : How to determine if request or crawl is from Google robots
+6.7k Golang : Go as a script or running go with shebang/hashbang style
+10.5k Golang : Date and Time formatting
+24.3k Golang : Get file last modified date and time
+6.7k Golang : Send data to /dev/null a.k.a blackhole with ioutil.Discard
+13.9k Golang : Compare floating-point numbers
+24k Golang : Get and Set User-Agent examples
+19.6k Golang : Get executable name behind process ID example
+6.5k Golang : How to control fmt or log print format?
+16.3k Nginx + FastCGI + Go Setup.
+9.8k Javascript : Prompt confirmation before exit
+6k Golang : Build and compile multiple source files