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

Register

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

Split String into Array by Words

Home ios Split String into Array by Words

Split String into Array by Words

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

Splitting a string into an array based on the word sounds daunting initially, however is quite simple. To achieve this follow the sample code below:

        let sentence = "Hello im Seemu"
        let wordsArray = sentence.components(separatedBy: " ")
        
        let wordFirst = wordsArray[0] // Hello
        let wordSecond = wordsArray[1] // im
        let wordThird = wordsArray[2] // Seemu
        
        print(wordFirst)
        print(wordSecond)
        print(wordThird)

This will result in the output as follows:

Hello
im
Seemu

One use of this sort of splitting is getting a first name and last name separately as follows:

        let name: String = "Bob Smith"
        let wordsName = name.components(separatedBy: " ")
        
        let firstName = wordsName[0]
        let lastName = wordsName[1]
        
        print(firstName) // Bob
        print(lastName) // Smith

This will give you the following output:

Bob
Smith

Just note, be careful if someone has a middle name, example “Bob Joey Smith” as you will get unexpected results!

DownloadSourceCode

 

Tags: arraynamessplitstring
1
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

Swift Extensions – speed up app development

May 24, 2017

Swift Extensions are quite a powerful feature and Swift and[...]

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

Apr 18, 2016

To get n character of a String in Swift you[...]

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