Swift String Concepts

String is the special data type in programming languages, the sequence of characters is String. In Swift string variables are declared using String syntax.

And character is an element in String which is an another data type in a programming language.  In swift, character syntax is Character. Ok, don’t worry I won’t leave you without telling an example for that.You will get an example in the String mutability section.

Mutable, Immutable String:

String mutability is, how we can alter the string variable. if a string is declared with var keyword if you want to get to know about var and let, please check our variable and constants tutorial. Let’s come back to our point. If a string variable is declared with var keyword, then it is a mutable string which means we can change the value of that variable at any time like we have NSMutableString in Objective C. In Swift it is very simple right.

just declare as

var mutableString : String = “Mutable string as a keyword is var”

whereas if a string is declared with let keyword string, then it is constant and we can not change the value of that string object.

let immutableString: String = “String can not be modified”

This is just similar to NSString in Objective C.

if we try to change the value of the string, the compiler will throw an error.

immutableString = “modifying constant String” // compiler error for modifying constants.

whereas we can change the value of mutable string which is having var keyword.

mutableString = “i am able to modify var string” // compiler not saying any error.

Characters In String:

As we states earlier,string is a collection of characters , we can access characters in a string using for-in . I will write tutorial about for-in in control flow chapter soon.

let myString = “characters list”

for c in myString {

print(\(c)) // it prints character in that string one by one.

}

String interpolation:

String interpolation is nothing , constructing a string from multiple data type like int,character or other literal. Below is an example for that.

let interpolationStr = \(3) + \(2) = \(5)”

print(interpolationStr) // prints 3 + 2 = 5

where, 3 , 2 and 5 are Int.

String concatenation:

String concatenation is very simple in Swift, we have already mentioned this in operators chapter. Arithmetic plus ‘+’ operator can be used for string concatenation.

let concatString = “string1” + “string2”

print(\(concatString)) // prints as string1string2 as + concatenates both strings.

Is Swift String Unicode compliant?

Unicode is an International standard format, if a text file is not supporting that then it will not be recognized by other programming languages. It clears that Unicode is for reading, encoding text from different writing systems. Swift is also Unicode compliant.

How does this Unicode work? Unicode has some background strategy that each character will have a scalar value that is Unicode scalar. For example, U+0061 is for ‘a’.

Modifying the String:

Swift provides a couple of options to access or modify the strings. It could be either through methods or subscript.

First of all, we need to initialize the string,

let myFirstString = “”

let myString = String()

In both ways we can initiate the string, both are equal and has an empty string.

Empty String:

Swift provides a method to check if the string is empty or not. Below is an example for that

if myString.isEmpty {

    print(“String is Empty”)

}

isEmpty() method is used to check the empty string.

String Indices:

As we all know, the index is for pointing the position. Since string in a collection of characters, String has a property called index. An index gives the position of that character.

Three key points , we should keep in mind are startIndex , endIndex, offSetBy. Let me explain one by one .

startIndex gives you the first character position of the String, for example

let myString = “abcde”

if myString.isEmpty {

    print(“String is Empty”)

}

print(\(myString[myString.startIndex])”) // prints a

As String is a collection of characters , we are getting the first index using startIndex and accessing the first character from the String.

endIndex gives you the last character position of the String, for example

let myString = "abcde"

if myString.isEmpty {

    print("String is Empty")

}

print("\(myString[myString.endIndex])") // prints e

As String is a collection of characters, we are getting the last index using endIndex and accessing the last character from the String.

Note: please keep in mind that we can not access a character in String using integers

Thats why our next key word offSetBy comes here, which advances the index position from startIndex in the below example

print(\(myString[myString.index(myString.startIndex, offsetBy: 3)])”

Swift-string-learn
Swift String index and empty

The above print statement prints ‘d’ , since we are setting the offset value 3 from startIndex.

Hope that I have given the basic stuff about String in Swift. Please share and raise your feedback in comments

Leave a Reply

Your email address will not be published. Required fields are marked *