How do you concatenate strings in Swift?
James Craig
Published Jan 14, 2026
String values can be added together (or concatenated) with the addition operator ( + ) to create a new String value:
- let string1 = "hello"
- let string2 = " there"
- var welcome = string1 + string2.
- // welcome now equals "hello there"
Can you add strings in Swift?
In the apple documentation for swift it states that you concatenate 2 strings by using the additions operator e.g. : let string1 = "hello" let string2 = " there" var welcome = string1 + string2 4 // welcome now equals "hello there"
Which of the following are acceptable ways to concatenate strings?
You can add a string in these ways:
- str += ""
- str = str + ""
- str = str + str2.
- str = "" + ""
- str = "\(variable)"
- str = str + "\(variable)"
How do I make a line break in Swift?
5 Answers
- If you are setting it to a UILabel's text property, make sure that the UILabel's numberOfLines = 0, which allows for infinite lines.
- In some networking use cases, use \r\n instead, which is the Windows newline.
How do you check if two strings are equal in Swift?
Test for string equality in Swift
In Swift, you can check for string and character equality with the "equal to" operator ( == ) and "not equal to" operator ( != ).
24 related questions foundWhat is string interpolation in Swift?
String interpolation is a way to construct a new String value from a mix of constants, variables, literals, and expressions by including their values inside a string literal. You can use string interpolation in both single-line and multiline string literals.
What is index in Swift?
Index for every String is that Characters in Swift are not all the same length under the hood. A single Swift Character might be composed of one, two, or even more Unicode code points. Thus each unique String must calculate the indexes of its Characters.
What does print mean in Swift?
Printing is used to output text directly to the console.
What is an attributed string?
Overview. Attributed strings are character strings that have attributes for individual characters or ranges of characters. Attributes provide traits like visual styles for display, accessibility for guided access, and hyperlink data for linking between data sources.
Which operator is used to concatenate two strings?
Concatenation operators join multiple strings into a single string. There are two concatenation operators, + and & . Both carry out the basic concatenation operation, as the following example shows.
Which operator is used to concatenate two strings Java?
Using the + operator is the most common way to concatenate two strings in Java. You can provide either a variable, a number, or a String literal (which is always surrounded by double quotes).
Which operator is suitable for the concatenation of a string class?
The ampersand symbol is the recommended concatenation operator. It is used to bind a number of string variables together, creating one string from two or more individual strings.
What does string mean in Swift?
A string is a series of characters, such as "Swift" , that forms a collection. Strings in Swift are Unicode correct and locale insensitive, and are designed to be efficient. The String type bridges with the Objective-C class NSString and offers interoperability with C functions that works with strings.
How do I get the last character of a string in Swift?
To get the last character of a string, we can use the string. last property in Swift. Similarly, we can also use the suffix() method by passing 1 as an argument to it. The suffix() method can also be used to get the last n characters from a string.
What is Swift character set?
In Swift, a Character is an extended grapheme cluster (really just a String with a length of 1) that comprises one or more scalar values. CharacterSet stores those underlying Unicode. Scalar values, rather than Character values, as the name might imply.
What is separator in Swift?
separator. A string to print between each item. The default is a single space ( " " ).
What is dump in Swift?
Dumps the given object's contents using its mirror to standard output and specified output stream. We can use also Dump for the same purpose as we used to print() but print() only prints the class name while in the case of dump() prints the whole class hierarchy at Xcode debug console.
How do I debug in Xcode?
When you run an application in Xcode, the debugger is automatically started and attached to the process of the application. Click the Run button in the top left or press Command + R. From the moment the application is up and running, we can start inspecting the process and, if necessary, debug it.
What is Uiviewrepresentable?
A wrapper for a UIKit view that you use to integrate that view into your SwiftUI view hierarchy. iOS 13.0+ iPadOS 13.0+
How do you wrap text in UILabel?
If you set numberOfLines to 0 (and the label to word wrap), the label will automatically wrap and use as many of lines as needed. If you're editing a UILabel in IB, you can enter multiple lines of text by pressing option + return to get a line break - return alone will finish editing.
What is Equatable in Swift?
The Equatable protocol is what allows two objects to be compared using == , and it's surprisingly easy to implement because Swift does most of the work for you by default.
What is a subscript in Swift?
Subscripts are used to access information from a collection, sequence and a list in Classes, Structures and Enumerations without using a method. These subscripts are used to store and retrieve the values with the help of index without the use of separate method.
What is Startindex in Swift?
The position of the first character in a nonempty string.
What is type inference in Swift?
Type inference enables a compiler to deduce the type of a particular expression automatically when it compiles your code, simply by examining the values you provide. Because of type inference, Swift requires far fewer type declarations than languages such as C or Objective-C.