HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to SwiftVariablesData TypesOperatorsStringsArraysConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 Array & Dictionary
🔀 Condition & Loop
🧩 Function
Swift›Strings

Strings

Concatenation, interpolation, length (count), and uppercase/lowercase conversion.

Simple enough for a 5-year-old to understand

Concatenation and interpolation

Example code
1
let name = "Alex"
2
let age = 36
3
print("\(name) \(age) years old")
Alex is 36 years old
Output / InputAlex is 36 years old
4
print("Hello " + name)
Hello Alex (concatenation with +)
Output / InputHello Alex (concatenation with +)
Output / Input
💡 Hint: You cannot add a number to text with +; embed it with \(...).

About this topic

In Swift, texts are of String type and are written within double quotes. String is designed to handle Unicode correctly: emojis, Turkish characters, and combining marks count as a single character. This accuracy is the reason why you cannot index text like a number.

There are two ways to combine texts: appending with the plus operator or using string interpolation. The second one is more readable because it does not require type conversion when mixing numbers and text.

Multi-line texts are written with three pairs of double quotes. The opening and closing marks stand on their own lines, and the indentation of the closing mark determines where the text's indentation is counted from — this is a useful detail specific to Swift.

What you will learn

  • Creating and concatenating strings
  • String interpolation
  • Length and search operations
  • Case transformations
  • Writing multi-line strings

Who is this for?

  • Those who will display text to the user
  • Developers accustomed to reading text by index in other languages
  • Those who want to understand Unicode behavior

When to use?

Every label shown in the UI, every input received from the user, and every field sent to the server is a String; text operations make up a large portion of application code.

For fixed-format reports and templates, multi-line texts eliminate escape character clutter.

Common mistakes

Wrong

let name = "Ayşe"
print(name.length)

In Swift, String does not have a length property, you will get the error "value of type 'String' has no member 'length'". The character count is obtained with count. This name difference often misleads those coming from Java or JavaScript.

Correct

let name = "Ayşe"
print(name.count)

Wrong

let text = "merhaba"
print(text[0])

A String cannot be indexed with an Int; the error "'subscript(_:)' is unavailable: cannot subscript String with an Int" is received. The reason is Unicode: because a character can take a variable number of bytes in memory, the nth character cannot be reached in constant time. Swift explicitly prevents this instead of hiding it.

Correct

let text = "merhaba"
print(text[text.startIndex])

Wrong

let count = 5
let message = "Toplam: " + count

String and Int cannot be added directly; it gives the error "binary operator '+' cannot be applied to operands of type 'String' and 'Int'". You need to convert the number to text or use string interpolation. The latter is both shorter and handles type conversion for you.

Correct

let count = 5
let message = "Toplam: \(count)"

Frequently asked questions

Why can't a String be indexed with an Int?

Because Swift counts characters as the unit the user sees, and these units do not take up equal space in memory. If it allowed indexing like an array, the operation would not run in constant time and would silently slow down. Swift makes this cost visible rather than hiding it.

How do I reach a specific character?

You can calculate the position with index(_:offsetBy:) and use that index, or convert it to a character array with Array(text). If you are going to access it multiple times in a loop, converting it to an array is more efficient because you don't count from the beginning on every access.

Is == enough when comparing two texts?

Yes, in Swift == compares the contents of texts and also takes Unicode equivalence into account. For case-insensitive comparison, you need to convert both sides with lowercased() or use caseInsensitiveCompare.

Summary

Length is obtained with count, there is no length.

String cannot be indexed with Int, String.Index is used.

String interpolation is used to include a number in the text.

Multi-line texts are written with three pairs of double quotes.

== compares content.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code