Swift Strings

Strings are text. Use + to join, interpolation with \(value) to insert values, and properties like count and isEmpty to inspect them.


Basics

Create, combine, and inspect strings with concatenation, interpolation, and common properties.

Example

let s1 = "Hello"
let s2 = "Swift"
print(s1 + " " + s2)
print("\(s1), \(s2)!")
let word = "Swift"
print(word.count)
print(s1.isEmpty)

This example shows concatenation, interpolation, count, and isEmpty.



Substring and Case

Get substrings with indices. Uppercased/lowercased creates new strings without changing the original.

Example

let text = "Swift"
let start = text.startIndex
let end = text.index(start, offsetBy: 3)
let sub = text[start..<end]  // "Swi"
print(sub)
print(text.uppercased())

Comments

Leave a Reply

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