Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Swift Dictionary

  1. Home
  2. Swift Dictionary
  3. String.count / isEmpty / String Literals

String.count / isEmpty / String Literals

Swift strings use the String type and are fully Unicode-compliant. Use count to get the number of characters, string interpolation (\()) to embed variables, and multi-line string literals (""") for multi-line text.

Syntax

// Declaring strings
let str = "text"
var mutable = "mutable string"

// String interpolation
let message = "The value is \(variable)"

// Multi-line string literal
let multiline = """
    Line 1
    Line 2
    Line 3
    """

// Get character count
str.count

// Check if empty
str.isEmpty

Methods and Properties

Method / PropertyDescription
string.countReturns the number of characters in the string (in Unicode extended grapheme clusters).
string.isEmptyReturns a Bool indicating whether the string is empty (count == 0).
\(variable)Embeds the value of a variable or expression into a string using string interpolation.
"""..."""Defines a multi-line string literal.
string.utf8.countReturns the number of UTF-8 bytes (may differ from count).
string.unicodeScalars.countReturns the number of Unicode scalar values.
string + stringConcatenates two strings.
string += stringAppends to a string (only for var).

Sample Code

// String basics
let greeting = "Hello"
print(greeting.count)    // 5
print(greeting.isEmpty)  // false

let empty = ""
print(empty.isEmpty)     // true

// String interpolation
let name = "Taro"
let age = 25
let intro = "\(name) is \(age) years old. Next year they will be \(age + 1)."
print(intro)

// Multi-line string literal
let poem = """
    Spring is the dawn,
    Summer is the night,
    Autumn is the dusk,
    Winter is the early morning.
    """
print(poem)

// String concatenation
var message = "Hello"
message += ", World"
message = message + "!"
print(message)  // Hello, World!

// Unicode and character count difference
let emoji = "👨‍👩‍👧‍👦"  // Family emoji (counts as 1 character)
print(emoji.count)         // 1
print(emoji.utf8.count)    // Byte count (emoji uses multiple bytes)

// Practical character count validation
func validateUsername(_ username: String) -> Bool {
    return !username.isEmpty && username.count <= 20
}
print(validateUsername("Taro"))  // true
print(validateUsername(""))      // false

Notes

Swift's String.count counts characters in units of Unicode extended grapheme clusters. This means emoji (👨‍👩‍👧‍👦) and combining characters (é = e + ´) each count as a single character.

isEmpty is more efficient than checking count == 0. Use utf8.count when you need the byte count, and unicodeScalars.count when you need the Unicode scalar count. For multilingual apps, it is recommended to always use count (grapheme clusters).

For string searching and matching, see String.hasPrefix() / hasSuffix() / contains().

If you find any errors or copyright issues, please .