如何检查String在Swift中以(前缀)开头或以(后缀)结尾的内容 [英] How to check what a String starts with (prefix) or ends with (suffix) in Swift

查看:2511
本文介绍了如何检查String在Swift中以(前缀)开头或以(后缀)结尾的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

我也会喜欢获取前缀和后缀字符串。我可以找到一个子字符串,如回答这里这里,但范围在Swift中是如此痛苦。

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

有更简单的方法吗?

(我在阅读文档,由于我的搜索字词没有提供SO答案,我在这里添加了我的Q& A.)

(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.)

推荐答案

更新为Swift 4

您可以使用 hasPrefix(_:) hasSuffix(_ :) 测试eq的方法使用另一个字符串的uality。

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

let str = "Hello, playground"

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

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



获取实际前缀和后缀子串



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

let str = "Hello, playground"

方法1 :(推荐)前缀(Int)后缀(Int)

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

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

在我看来,这是更好的方法。与下面的方法2和3不同,如果索引超出范围,此方法不会崩溃。它只会返回字符串中的所有字符。

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.

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.

方法2:前缀(upto :) 后缀(来自:)

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

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

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 answer on 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

如果你想防止越界,你可以使用 limitedBy (再次参见此答案)。

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

方法3:下标

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

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

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

    • Strings and Characters documentation
    • 这篇关于如何检查String在Swift中以(前缀)开头或以(后缀)结尾的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆