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.split() / joined() / String Index

String.split() / joined() / String Index

Swift's split(separator:) splits a string and returns an array of Substring values. joined(separator:) joins the elements of an array into a single string. Use String.Index for index-based access.

Syntax

// Split a string (returns an array of Substring)
string.split(separator: "character")
string.split(separator: "character", maxSplits: limit)
string.split(separator: "character", omittingEmptySubsequences: false)

// Join an array
array.joined(separator: "delimiter")

// Index operations
string[string.startIndex]
string[string.index(string.startIndex, offsetBy: n)]
string.prefix(n)
string.suffix(n)
string.dropFirst(n)
string.dropLast(n)

Method List

MethodDescription
string.split(separator:)Returns an array of Substring values split by the given separator.
string.split(separator: maxSplits:)Splits the string up to a specified maximum number of times.
array.joined(separator:)Returns a string by joining the array elements with the given separator.
string.prefix(n)Returns a Substring containing the first n characters.
string.suffix(n)Returns a Substring containing the last n characters.
string.dropFirst(n)Returns a Substring with the first n characters removed.
string.dropLast(n)Returns a Substring with the last n characters removed.
string.index(startIndex, offsetBy: n)Returns the String.Index that is n characters after startIndex.

Sample Code

// split: split a string
let sentence = "Swift は 型安全 な 言語 です"
let words = sentence.split(separator: " ")
print(words)  // ["Swift", "は", "型安全", "な", "言語", "です"]
print(type(of: words[0]))  // Substring

// Convert to String
let wordStrings = words.map { String($0) }

// Limit splits with maxSplits
let csv = "A,B,C,D,E"
let parts = csv.split(separator: ",", maxSplits: 2)
print(parts)  // ["A", "B", "C,D,E"]

// joined: join an array into a string
let fruits = ["りんご", "みかん", "ぶどう"]
let joined = fruits.joined(separator: "、")
print(joined)  // りんご、みかん、ぶどう

let path = ["Users", "taro", "Documents"]
print(path.joined(separator: "/"))  // Users/taro/Documents

// prefix / suffix / dropFirst / dropLast
let text = "Hello, World!"
print(text.prefix(5))    // Hello
print(text.suffix(6))    // World!
print(text.dropFirst(7)) // World!
print(text.dropLast(1))  // Hello, World

// Accessing characters with String.Index
let str = "あいうえお"
let startIdx = str.startIndex
let thirdIdx = str.index(startIdx, offsetBy: 2)  // 3rd character
print(str[thirdIdx])  // う

// Slice with a range
let range = str.index(startIdx, offsetBy: 1) ..< str.index(startIdx, offsetBy: 4)
print(str[range])  // いうえ

Notes

split(separator:) returns an array of Substring values. A Substring is a view that shares memory with the original string. If you need to store the result independently, copy it with String($0).

Swift string indices are of type String.Index, not integers. Because string length depends on Unicode, random access is not O(1). Be mindful of performance when doing many index-based lookups. prefix() and suffix() handle the index arithmetic internally and improve readability, so prefer them when possible.

For string basics such as count and isEmpty, see String.count / isEmpty / String Literals.

If you find any errors or copyright issues, please .