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
+5.5k PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)
+25.9k Mac/Linux and Golang : Fix bind: address already in use error
+37.9k Golang : Read a text file and replace certain words
+8.7k Golang : Gorilla web tool kit schema example
+7k Restart Apache or Nginx web server without password prompt
+5.9k PHP : How to check if an array is empty ?
+27.2k Golang : Convert CSV data to JSON format and save to file
+13.9k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+8.5k Golang : Another camera capture GUI application with GTK and OpenCV
+8k Golang : Variadic function arguments sanity check example
+6.7k Unix/Linux : How to fix CentOS yum duplicate glibc or device-mapper-libs dependency error?
+17.7k Golang : Login and logout a user after password verification and redirect example