乐闻世界logo
搜索文章和话题

What are common string processing methods in Swift? How to perform string concatenation, substring, replacement, and search?

2月21日 15:10

What are common string processing methods in Swift? How to perform string concatenation, substring, replacement, and search?

Swift provides rich string processing methods, including concatenation, substring, replacement, search, and other operations. Swift's String type is a value type that supports Unicode characters.

String Concatenation:

swift
// Using + operator let str1 = "Hello" let str2 = "World" let combined = str1 + ", " + str2 // "Hello, World" // Using += operator var greeting = "Hello" greeting += ", World" // "Hello, World" // Using string interpolation let name = "John" let age = 30 let message = "My name is \(name) and I'm \(age) years old" // Using append method var text = "Hello" text.append(", World") // "Hello, World"

String Substring:

swift
let str = "Hello, World" // Using prefix and suffix let prefix = str.prefix(5) // "Hello" let suffix = str.suffix(6) // "World" // Using dropFirst and dropLast let withoutFirst = str.dropFirst() // "ello, World" let withoutLast = str.dropLast() // "Hello, Worl" // Using indices let startIndex = str.startIndex let endIndex = str.index(str.startIndex, offsetBy: 5) let substring = str[startIndex..<endIndex] // "Hello" // Using range let range = str.range(of: "World")! let substring2 = str[range] // "World"

String Replacement:

swift
let str = "Hello, World" // Replace single character let replaced = str.replacingOccurrences(of: "World", with: "Swift") // "Hello, Swift" // Using closure for replacement let replaced2 = str.replacingOccurrences(of: "o", with: "O") // "HellO, WOrld" // Using closure for complex replacement let replaced3 = str.replacingOccurrences(of: "[aeiou]", with: "", options: .regularExpression) // "Hll, Wrld"

String Search:

swift
let str = "Hello, World" // Check if contains let containsHello = str.contains("Hello") // true let containsSwift = str.contains("Swift") // false // Find substring if let range = str.range(of: "World") { print("Found at range: \(range)") } // Find prefix and suffix let hasPrefix = str.hasPrefix("Hello") // true let hasSuffix = str.hasSuffix("World") // true // Find character let firstIndex = str.firstIndex(of: ",") // Optional(String.Index)

String Splitting:

swift
let str = "apple,banana,orange" // Split by character let components = str.components(separatedBy: ",") // ["apple", "banana", "orange"] // Split by character set let whitespace = "Hello World" let words = whitespace.components(separatedBy: .whitespaces) // ["Hello", "World"]

String Case Conversion:

swift
let str = "Hello, World" let uppercased = str.uppercased() // "HELLO, WORLD" let lowercased = str.lowercased() // "hello, world" let capitalized = str.capitalized // "Hello, World"

String Trimming:

swift
let str = " Hello, World " let trimmed = str.trimmingCharacters(in: .whitespaces) // "Hello, World" let trimmed2 = str.trimmingCharacters(in: .whitespacesAndNewlines) // "Hello, World"

String Length and Characters:

swift
let str = "Hello, World" let count = str.count // 13 let isEmpty = str.isEmpty // false let firstChar = str.first // Optional("H") let lastChar = str.last // Optional("d") let chars = Array(str) // ["H", "e", "l", "l", "o", ",", " ", "W", "o", "r", "l", "d"]

Best Practices:

  1. Use string interpolation to improve readability
  2. Pay attention to string index usage
  3. Use contains, hasPrefix, hasSuffix for quick checks
  4. Use replacingOccurrences for string replacement
  5. Be aware of Unicode character handling
标签:Swift