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
+11.1k Golang : Byte format example
+6.6k Golang : Calculate pivot points for a cross
+7k Golang : File system scanning
+4.3k Java : Generate multiplication table example
+13.7k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+8.2k PHP : How to parse ElasticSearch JSON ?
+32.4k Golang : Regular Expression for alphanumeric and underscore
+20.8k Golang : Clean up null characters from input data
+29.6k Golang : How to get HTTP request header information?
+11.5k Golang : GTK Input dialog box examples
+23k Golang : Get ASCII code from a key press(cross-platform) example
+9.9k Golang : Check a web page existence with HEAD request example