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 Literal, Interpolation

Use string literals and string interpolation. Combine strings and append to them.
String literals. We surround a string literal with double quotes. In Swift a string literal can use string interpolation—a variable can be inserted into it.
Literals, notes. We use the let keyword to declare these strings, as they are constants. To have a string that can be modified, please declare it with the var keyword (not let).
Combine strings. We can use the plus operator to combine two strings. This is also called concatenation. The resulting string is printed to the console.Print
Swift program that combines two strings // Two string literals. let value = "cat " let value2 = "and dog" // Combine (concatenate) the two strings. let value3 = value + value2 print(value3) Output cat and dog
Append. A string that is declared with the var keyword can be appended to. A variable can be reassigned. Here we append to a string and use print to display it.

Also: The append() func can be used to add a character on the end of an existing string.

Swift program that appends to string // This string can be changed. var value = "one" // Append data to the var string. value += "...two" print(value) Output one...two
String interpolation. With this syntax we can insert variables (like Ints or other Strings) into a place in a String. No complex string operations are needed.

Tip: To specify an interpolated variable, use an escaped parenthesis. Then specify the variable name.

Swift program that uses string interpolation let name = "dotnetCodex" let id = 100 // Use string interpolation. // ... Create a string with variable values in it. let result = "Location: \(name), ID: \(id)" print(result) Output Location: dotnetCodex, ID: 100
A summary. String literals are used in nearly every program. They are used in print statements. We use them to create text output in Swift.
© 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