| by Arround The Web | No comments

Swift String – Append()

In this Swift string tutorial, we will see what append() method does.

Consider a scenario

Sravan is working in an organization. Being an IOS Programmer, he wants to join/add the string to the existing string. Finally, he came to know that Swift supports the append() method that adds the string to the actual string. Finally, he did it and documented the approach.

Let’s see what he documented.

append()

append() in swift string is used to add the string to the existing string.

Syntax:

swift_string.append(string)

Parameters:

It takes only one string as a parameter.

To understand this method much better, we have to see the examples in detail.

Example 1:

Let’s create a string named swift_string and append another string.

//consider the swift string.

var swift_string:String = "This is my actual string"

print("Original string - ",swift_string)

//append the string - "Added string"

swift_string.append(" Added string")

print("Final string - ",swift_string)

Output:

Explanation:

Line 2:

We created a string named swift_string that holds: “This is my actual string”.

Line 7:

Now, we are appending a string – ” Added string” to the swift_string.

Line 9:

Finally, we are displaying the whole string.

Example 2:

Let’s create a string named swift_string and append a character.

//consider the swift string.

var swift_string:String = "This is my actual string"

print("Original string - ",swift_string)

//append the string with single character - "$"

swift_string.append("$")

print("Final string - ",swift_string)

Output:

Explanation:

Line 2:

We created a string named swift_string that holds: “This is my actual string”.

Line 7:

Now, we are appending a string that has a single character-$.

Line 9:

Finally, we are displaying the whole string.

Example 3:

Let’s create a string named swift_string and append another string individually.

//consider the swift string.

var swift_string:String = "one string"

print("Original string - ",swift_string)

//append the string - "two string"

swift_string.append("two string")

print("Appended string 1- ",swift_string)

//append the string - "three string"

swift_string.append("three string")

print("Appended string 2- ",swift_string)

//append the string - "four string"

swift_string.append("four string")

print("Appended string 3- ",swift_string)

Output:

Explanation:

Line 2:

We created a string named swift_string that holds – “one string”.

Line 7:

Now, we are appending a string – “two string” to the swift_string.

The actual string becomes – “one stringtwo string”.

Line 12:

Now, we are appending a string – “three string” to the swift_string.

The actual string becomes – “one stringtwo stringthree string”.

Line 17:

Now, we are appending a string – “four  string” to the swift_string.

So, the final string is – “one stringtwo stringthree stringfour string”.

Conclusion

In this way, Sravan documented this method by explaining three examples with different scenarios. append() in swift string is used to add the string to the existing string. It is not possible to append more than one string at a time to their existing string using append() method. If you do that, you will get an error – “overloads for append ”.

Share Button

Source: linuxhint.com

Leave a Reply