TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to SWIFT

Swift String Append Example: reserveCapacity

Use the append method on strings to add characters and other strings.
Append. With this method we can add characters or another string to a string. We must use the var keyword to have a string that can be modified.
With the plus operator, we can also append and concatenate strings. But sometimes using append() is clearer in code (it is obvious we are not adding ints).String Literals
Characters. This example creates a Character array and an empty string. It then adds those Characters to the string with append().

Tip: For complex string mutations, building up a new string based on characters is sometimes easiest.

For: We use a for-in loop to iterate over the three characters in the Character array.

Then: We add each character 3 times to the result string. We finally print the string to the console.

Print
Swift program that uses append, adds Characters to string let chars: [Character] = ["A", "B", "C"] var result = String() // Loop over all Characters in the array. for char in chars { for var i in 0..<3 { // Append a Character to the string. result.append(char) } } // Write the result String. print(result) Output AAABBBCCC
Append strings. Here we append a string to another existing string. The end result is printed to the console. Append() can handle characters or entire strings at once.
Swift program that appends strings var result = "cat" // The append method can append a string. result.append(" and dog") print(result) Output cat and dog
ReserveCapacity. With this method we can increase the memory used for a string. Then using append() on the string can proceed without resizing the string—this improves performance.

Here: We use reserveCapacity with an argument of 100, which accommodates 100 ASCII characters.

Swift program that uses reserveCapacity // Use reserveCapacity to ensure enough memory. // ... This reduces resizing. var pattern = String() pattern.reserveCapacity(100) // Add characters to string. for i in 0..<100 { pattern += "a" } // Print first 20 characters of the string. print(pattern[pattern.startIndex..<pattern.index(pattern.startIndex, offsetBy: 20)]) Output aaaaaaaaaaaaaaaaaaaa
A summary. With append we change strings to have more characters or strings on their ends. This is useful for building up strings from other data sources.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf