• Home
  • About
  • Our Apps
  • Learn
  • Contact
Login

Register

Login
Seemu Apps Seemu Apps
  • Home
  • About
  • Our Apps
  • Learn
  • Contact

Get nth character of a String & get a Substring from a String

Home ios Get nth character of a String & get a Substring from a String

Get nth character of a String & get a Substring from a String

Apr 18, 2016 | Posted by Andrew | ios, swift, tutorial, xcode |

To get n character of a String in Swift you simply use Index, same to get a substring.

The code below does the following:

  • Get the nth character from a String (1)
  • Get the nth character from the End of a String (2)
  • Get a Substring from  a string from X position to Y position (3)
  • Get a Substring starting a nth character to the end of the String (4)
  • Get a Substring starting at the start of a string to the nth character (5)
        let str = "Seemu Apps"
        
        // 1
        // Get a character at X position (index)
        let index = str.characters.index(str.startIndex, offsetBy: 4)
        let startChar = str[index] // returns Character 'u'
        print(startChar)
        
        // 2
        // Get a character at X position (index) starting from the end of the string
        let endIndex = str.characters.index(str.endIndex, offsetBy: -2) // Goes to the end of the string and back to characters
        let endChar = str[endIndex] // returns Character "p"
        print(endChar)
        
        // 3
        // Get the substring, starting from index and ending at endIndex
        let subString = str[(index ..< endIndex)]
        print(subString) // returns "u Ap"
        
        // 4 & 5
        // Get the substring starting from Index until the End of the String
        let indexToEnd = str.substring(from: index)
        // Get the substring starting frmo the Start of the String to the Index
        let startToIndex = str.substring(to: index)
        print(indexToEnd) // returns String "u Apps"
        print(startToIndex) // returns String "Seem"

It will output the results to the console as you can see in the comments. You can download the source code below if you wish to see it in action and play around with it!

DownloadSourceCode

a

Tags: characterindexstringsubstring
0
Share

About Andrew

Andrew is a 24 year old from Sydney. He loves developing iOS apps and has done so for two years.

You also might be interested in

Converting a NSDate to a String and String to a NSDate

Mar 30, 2016

Converting a NSDate to a String and back again is[...]

Split String into Array by Words

Apr 5, 2016

Splitting a string into an array based on the word[...]

Convert String to Number (Integer, Float, Double)

Apr 21, 2016

Converting a string to a number is simple thanks to[...]

Welcome

Hi I am Andrew and welcome to Seemu Apps! Have a look around, we provide tutorials for primarily iOS apps.
Bluehost website hosting discount

Seemu’s Studio Setup

Blue Yeti Microphone
Rode Stand
Spider Shock Mount
Mac Keyboard Cover
Screenflow - recording software

Contact Us

We're currently offline. Send us an email and we'll get back to you, asap.

Send Message

Footer

:)

© 2025 · Your Website. Theme by HB-Themes.

  • Home
  • About
  • Our Apps
  • Learn
  • Contact
Prev Next