ios 如何在 Swift 中检查字符串以(前缀)开头或以(后缀)结尾

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32967445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:55:40  来源:igfitidea点击:

How to check what a String starts with (prefix) or ends with (suffix) in Swift

iosstringswift

提问by Suragch

I'm trying to test if a Swift string starts or ends with a certain value. These methods do not exist:

我正在尝试测试 Swift 字符串是否以某个值开头或结尾。这些方法不存在:

var str = "Hello, playground"
str.startsWith("Hello") // error
str.endsWith("ground") // error

I would also like to get the prefix and suffix strings. I could find a substring, as is answered hereand here, but ranges are such a pain in Swift.

我还想获得前缀和后缀字符串。我可以找到一个子字符串,正如这里这里所回答的那样,但是范围在 Swift 中是如此痛苦。

Is there an easier way to do it?

有没有更简单的方法来做到这一点?

(I stumbled across the answer when I was reading the documentation, and since an SO answer didn't come up for my search terms, I am adding my Q&A here.)

(我在阅读文档时偶然发现了答案,由于我的搜索词没有出现 SO 答案,我在这里添加我的问答。)

回答by Suragch

Updated for Swift 4

为 Swift 4 更新

Checking what a String starts with and ends with

检查字符串以什么开头和结尾

You can use the hasPrefix(_:)and hasSuffix(_:)methods to test equality with another String.

您可以使用hasPrefix(_:)hasSuffix(_:)方法来测试与另一个字符串的相等性。

let str = "Hello, playground"

if str.hasPrefix("Hello") { // true
    print("Prefix exists")
}

if str.hasSuffix("ground") { // true
    print("Suffix exists")
}

Getting the Actual Prefix and Suffix Substrings

获取实际的前缀和后缀子串

In order to get the actual prefix or suffix substring, you can use one of the following methods. I recommend the first method for it's simplicity. All methods use stras

为了获得实际的前缀或后缀子串,您可以使用以下方法之一。我推荐第一种方法,因为它很简单。所有方法都str用作

let str = "Hello, playground"

Method 1: (Recommended) prefix(Int)and suffix(Int)

方法一:(推荐)prefix(Int)suffix(Int)

let prefix = String(str.prefix(5)) // Hello
let suffix = String(str.suffix(6)) // ground

This is the better method in my opinion. Unlike the methods 2 and 3 below, this method will not crash if the indexes go out of bounds. It will just return all the characters in the string.

这是我认为更好的方法。与下面的方法 2 和 3 不同,如果索引越界,此方法不会崩溃。它只会返回字符串中的所有字符。

let prefix = String(str.prefix(225)) // Hello, playground
let suffix = String(str.suffix(623)) // Hello, playground

Of course, sometimes crashes are good because they let you know there is a problem with your code. So consider the second method below as well. It will throw an error if the index goes out of bounds.

当然,有时崩溃是好的,因为它们让您知道您的代码存在问题。因此,请考虑下面的第二种方法。如果索引超出范围,它将抛出错误。

Method 2: prefix(upto:)and suffix(from:)

方法2:prefix(upto:)suffix(from:)

Swift String indexes are tricky because they have to take into account special characters (like emoji). However once you get the index it is easy to get the prefix or suffix. (See my other answeron String.Index.)

Swift 字符串索引很棘手,因为它们必须考虑特殊字符(如表情符号)。但是,一旦获得索引,就很容易获得前缀或后缀。(请参阅在 上的另一个答案String.Index。)

let prefixIndex = str.index(str.startIndex, offsetBy: 5)
let prefix = String(str.prefix(upTo: prefixIndex)) // Hello

let suffixIndex = str.index(str.endIndex, offsetBy: -6)
let suffix = String(str.suffix(from: suffixIndex)) // ground

If you want to guard against going out of bounds, you can make an index using limitedBy(again, see this answer).

如果您想防止越界,您可以使用limitedBy(再次参见此答案)创建索引。

Method 3: subscripts

方法三:下标

Since String is a collection, you can use subscripts to get the prefix and suffix.

由于 String 是一个集合,因此可以使用下标来获取前缀和后缀。

let prefixIndex = str.index(str.startIndex, offsetBy: 5)
let prefix = String(str[..<prefixIndex]) // Hello

let suffixIndex = str.index(str.endIndex, offsetBy: -6)
let suffix = String(str[suffixIndex...]) // ground

Further Reading

进一步阅读

回答by Gobi M

Prefix and Suffix Equality

To check whether a string has a particular string prefix or suffix, call the string's hasPrefix(:) and hasSuffix(:) methods, both of which take a single argument of type String and return a Boolean value.

前缀和后缀相等

要检查字符串是否具有特定的字符串前缀或后缀,请调用字符串的 hasPrefix( :) 和 hasSuffix(:) 方法,这两个方法都采用 String 类型的单个参数并返回一个布尔值。

Apple Doc

苹果文档