Swift Language Strings and Characters

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Syntax

  • String.characters // Returns an Array of the characters in the String
  • String.characters.count // Returns the number of characters
  • String.utf8 // A String.UTF8View, returns the UTF-8 character points in the String
  • String.utf16 // A String.UTF16View, returns the UTF-16 character points in the String
  • String.unicodeScalars // A String.UnicodeScalarView, returns the UTF-32 character points in the String
  • String.isEmpty // Returns true if the String does not contain any text
  • String.hasPrefix(String) // Returns true if the String is prefixed with the argument
  • String.hasSuffix(String) // Returns true if the String is suffixed with the argument
  • String.startIndex // Returns the Index that corresponds to the first character in the string
  • String.endIndex // Returns the Index that corresponds to the spot after the last character in the string
  • String.components(separatedBy: String) // Returns an array containing the substrings separated by the given separator string
  • String.append(Character) // Adds the character (given as argument) to the String

Remarks

A String in Swift is a collection of characters, and by extension a collection of Unicode scalars. Because Swift Strings are based on Unicode, they may be any Unicode scalar value, including languages other than English and emojis.

Because two scalars could combine to form a single character, the number of scalars in a String is not necessarily always the same as the number of characters.

For more information about Strings, see The Swift Programming Language and the String Structure Reference.

For implementation details, see "Swift String Design"



Got any Swift Language Question?