We thought"Hello World"
This string is used as an example to determine whether it starts with Hello.
1. Use the hasPrefix(_:) method
Can use stringshasPrefix(_:)
Method check whether the string has a specified prefix:
let str = "Hello World" if ("Hello") { // true print("\(str) by Hello beginning") }
This method directly returns aBool
To determine whether it starts with a certain string.
2. The prefix function gets the prefix substring
Can be usedprefix(_:)
To get the prefix substring:
let str = "Hello World" let prefix = (5) if prefix == "Hello" { print("\(str) by Hello beginning") }
This method usesprefix
The function gets the first 5 characters and then compares it with "Hello".
3. The prefix(upTo:) function gets the prefix substring
Can be usedprefix(upTo:)
To get the prefix substring:
let str = "Hello World" let index = (, offsetBy: 5) let prefix = (upTo: index) if prefix == "Hello" { print("\(str) by Hello beginning") }
This method is first usedindex(_:, offsetBy:)
Get the subscript of the first five characters and use itprefix(upTo:)
The method of function obtaining the first 5 characters and finally comparing it with "Hello" is suitable for obtaining the first n characters of the string.
4. Use string interval index
First get the subscript of the first 5 characters, then get the values of the first 5 characters according to the subscript interval, and finally compare it with the corresponding string:
let str = "Hello World" let index = (, offsetBy: 5) let prefix = str[..<index] if prefix == "Hello" { print("\(str) by Hello beginning") }
5. Use conditions to obtain
Can be usedprefix(while:)
Get the prefix that meets the conditions:
let str = "Hello World" let prefix = { c in ! } if prefix == "Hello" { print("\(str) by Hello beginning") }
This method usesprefix(while:)
The function gets the string that specifies the specified condition (before the first space), and then compares it with "Hello" to get the result.
6. Use firstIndex/lastIndex
Can be combinedfirstIndex(of:)
orlastIndex(of:)
Gets the index of a specific character, thus obtaining the prefix:
let str = "Hello World" if let end = (of: " "), str[..<end] == "Hello" { print("\(str) by Hello beginning") }
Use firstfirstIndex(of:)
The method gets the position where the first space is located, and then obtains the specified prefix based on the subscript interval.
7. Use the prefix(through:) function
prefix(through:)
You can obtain a subset from the beginning to the specified position, which is similar to the second method above, except that the parameters here are passed in the subscript type:
let str = "Hello World" let index = (, offsetBy: 4) let prefix = (through: index) if prefix == "Hello" { print("\(str) by Hello beginning") }
The above are 7 common methods to obtain string prefixes, and you can choose the most suitable method according to your needs.
The above is the detailed content of the seven methods to obtain string prefixes in Swift. For more information about obtaining string prefixes in Swift, please pay attention to my other related articles!