빈 문자열 초기화:
var emptyString = ""
var anotherEmptyString = String()
리터럴로 초기화:
var greeting = "Hello, World!"
문자 접근 (Character):
let greeting = "Hello, World!"
let firstCharacter = greeting[greeting.startIndex] // "H"
let lastCharacter = greeting[greeting.index(before: greeting.endIndex)] // "!"
문자열 수정:
var welcome = "Hello"
welcome.append("!")
print(welcome) // "Hello!"
var welcome = "Hello"
welcome.insert(",", at: welcome.index(welcome.startIndex, offsetBy: 5))
print(welcome) // "Hello,"
var welcome = "Hello, World!"
welcome.remove(at: welcome.index(before: welcome.endIndex))
print(welcome) // "Hello, World"
var welcome = "Hello, World!"
let range = welcome.index(welcome.endIndex, offsetBy: -6)..<welcome.endIndex
welcome.replaceSubrange(range, with: "Swift!")
print(welcome) // "Hello, Swift!"
대소문자 변환:
let original = "Hello, World!"
let lowercased = original.lowercased()
let uppercased = original.uppercased()
print(lowercased) // "hello, world!"
print(uppercased) // "HELLO, WORLD!"
공백 제거:
let text = " Hello, World! "
let trimmed = text.trimmingCharacters(in: .whitespaces)
print(trimmed) // "Hello, World!"
부분 문자열:
let greeting = "Hello, World!"
let startIndex = greeting.index(greeting.startIndex, offsetBy: 7)
let endIndex = greeting.index(greeting.endIndex, offsetBy: -1)
let substring = greeting[startIndex..<endIndex]
print(substring) // "World"
문자열 포함 여부 확인:
let greeting = "Hello, World!"
let containsHello = greeting.contains("Hello")
print(containsHello) // true
문자열 접두사 및 접미사:
let greeting = "Hello, World!"
let hasPrefix = greeting.hasPrefix("Hello")
let hasSuffix = greeting.hasSuffix("World!")
print(hasPrefix) // true
print(hasSuffix) // true
문자열 비교:
let firstString = "Hello, World!"
let secondString = "hello, world!"
let isEqual = firstString == secondString
let isCaseInsensitiveEqual = firstString.lowercased() == secondString.lowercased()
print(isEqual) // false
print(isCaseInsensitiveEqual) // true
문자열 길이:
let greeting = "Hello, World!"
let length = greeting.count
print(length) // 13
문자 배열로 변환:
let greeting = "Hello, World!"
let charactersArray = Array(greeting)
print(charactersArray) // ["H", "e", "l", "l", "o", ",", " ", "W", "o", "r", "l", "d", "!"]
문자열을 정수형이나 실수형으로 변환:
let numberString = "12345"
let number = Int(numberString)
let floatString = "123.45"
let numberFloat = Float(floatString)
print(number) // Optional(12345)
print(numberFloat) // Optional(123.45)
문자열 분리:
let paragraph = "Hello, World! Welcome to Swift."
let sentences = paragraph.split(separator: " ")
print(sentences) // ["Hello,", "World!", "Welcome", "to", "Swift."]
문자열 연결:
let part1 = "Hello"
let part2 = "World"
let concatenated = part1 + ", " + part2 + "!"
print(concatenated) // "Hello, World!"
// 문자열 초기화 및 사용
var greeting = "Hello, World!"
print(greeting) // "Hello, World!"
// 문자열 접근
let firstCharacter = greeting[greeting.startIndex]
let lastCharacter = greeting[greeting.index(before: greeting.endIndex)]
print(firstCharacter) // "H"
print(lastCharacter) // "!"
// 문자열 수정
var welcome = "Hello"
welcome.append("!")
welcome.insert(",", at: welcome.index(welcome.startIndex, offsetBy: 5))
welcome.remove(at: welcome.index(before: welcome.endIndex))
welcome.replaceSubrange(welcome.index(welcome.endIndex, offsetBy: -6)..<welcome.endIndex, with: "Swift!")
print(welcome) // "Hello, Swift!"
// 대소문자 변환
let lowercased = greeting.lowercased()
let uppercased = greeting.uppercased()
print(lowercased) // "hello, world!"
print(uppercased) // "HELLO, WORLD!"
// 공백 제거
let text = " Hello, World! "
let trimmed = text.trimmingCharacters(in: .whitespaces)
print(trimmed) // "Hello, World!"
// 부분 문자열
let startIndex = greeting.index(greeting.startIndex, offsetBy: 7)
let endIndex = greeting.index(greeting.endIndex, offsetBy: -1)
let substring = greeting[startIndex..<endIndex]
print(substring) // "World"
// 문자열 포함 여부 확인
let containsHello = greeting.contains("Hello")
print(containsHello) // true
// 문자열 접두사 및 접미사
let hasPrefix = greeting.hasPrefix("Hello")
let hasSuffix = greeting.hasSuffix("World!")
print(hasPrefix) // true
print(hasSuffix) // true
// 문자열 비교
let firstString = "Hello, World!"
let secondString = "hello, world!"
let isEqual = firstString == secondString
let isCaseInsensitiveEqual = firstString.lowercased() == secondString.lowercased()
print(isEqual) // false
print(isCaseInsensitiveEqual) // true
// 문자열 길이
let length = greeting.count
print(length) // 13
// 문자 배열로 변환
let charactersArray = Array(greeting)
print(charactersArray) // ["H", "e", "l", "l", "o", ",", " ", "W", "o", "r", "l", "d", "!"]
// 문자열을 정수형이나 실수형으로 변환
let numberString = "12345"
let number = Int(numberString)
let floatString = "123.45"
let numberFloat = Float(floatString)
print(number) // Optional(12345)
print(numberFloat) // Optional(123.45)
// 문자열 분리
let paragraph = "Hello, World! Welcome to Swift."
let sentences = paragraph.split(separator: " ")
print(sentences) // ["Hello,", "World!", "Welcome", "to", "Swift."]
// 문자열 연결
let part1 = "Hello"
let part2 = "World"
let concatenated = part1 + ", " + part2 + "!"
print(concatenated) // "Hello, World!"