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.3k Golang : How to use if, eq and print properly in html template
+10.7k Golang : ISO8601 Duration Parser example
+5.8k Golang : Find change in a combination of coins example
+18.6k Golang : Set, Get and List environment variables
+10k Golang : Function wrapper that takes arguments and return result example
+12.4k Golang : Flush and close file created by os.Create and bufio.NewWriter example
+12.1k Golang : Clean formatting/indenting or pretty print JSON result
+14.7k Golang : GUI with Qt and OpenCV to capture image from camera
+26.4k Golang : Calculate future date with time.Add() function
+14.5k Golang : How to pass map to html template and access the map's elements
+11.7k Android Studio : Create custom icons for your application example
+10.5k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter