有什么办法可以替换Swift String上的字符吗?

本文翻译自:Any way to replace characters on Swift String?

I'm looking for a way to replace characters in a Swift String . 我正在寻找一种替换Swift String字符的方法。

Example: "This is my string" 示例: "This is my string"

I'd like to replace 我想更换 with + to get: "This+is+my+string" . +可以得到: "This+is+my+string"

How can I achieve this? 我该如何实现?


#1楼

参考:https://stackoom.com/question/1dXku/有什么办法可以替换Swift-String上的字符吗


#2楼

Did you test this : 您是否测试过:

var test = "This is my string"

let replaced = test.stringByReplacingOccurrencesOfString(" ", withString: "+", options: nil, range: nil)

#3楼

This answer has been updated for Swift 4 . 此答案已针对Swift 4进行了更新 If you're still using Swift 1, 2 or 3 see the revision history. 如果您仍在使用Swift 1、2或3,请参阅修订历史记录。

You have a couple of options. 您有两种选择。 You can do as @jaumard suggested and use replacingOccurrences() 您可以按照@jaumard的建议进行操作,并使用replaceOccurrences replacingOccurrences()

let aString = "This is my string"
let newString = aString.replacingOccurrences(of: " ", with: "+", options: .literal, range: nil)

And as noted by @cprcrack below, the options and range parameters are optional, so if you don't want to specify string comparison options or a range to do the replacement within, you only need the following. 就像下面@cprcrack所指出的那样, optionsrange参数是可选的,因此,如果您不想指定字符串比较选项或范围来在其中进行替换,则只需执行以下操作。

let aString = "This is my string"
let newString = aString.replacingOccurrences(of: " ", with: "+")

Or, if the data is in a specific format like this, where you're just replacing separation characters, you can use components() to break the string into and array, and then you can use the join() function to put them back to together with a specified separator. 或者,如果数据采用特定格式,例如您只是替换分隔符,则可以使用components()将字符串分成和数组,然后可以使用join()函数将其放回与指定的分隔符一起使用。

let toArray = aString.components(separatedBy: " ")
let backToString = toArray.joined(separator: "+")

Or if you're looking for a more Swifty solution that doesn't utilize API from NSString, you could use this. 或者,如果您正在寻找一种不使用NSString API的更快速的解决方案,则可以使用它。

let aString = "Some search text"

let replaced = String(aString.map {
    $0 == " " ? "+" : $0
})

#4楼

You can use this: 您可以使用此:

let s = "This is my string"
let modified = s.replace(" ", withString:"+")    

If you add this extension method anywhere in your code: 如果您在代码中的任何位置添加此扩展方法:

extension String
{
    func replace(target: String, withString: String) -> String
    {
       return self.stringByReplacingOccurrencesOfString(target, withString: withString, options: NSStringCompareOptions.LiteralSearch, range: nil)
    }
}

Swift 3: 斯威夫特3:

extension String
{
    func replace(target: String, withString: String) -> String
    {
        return self.replacingOccurrences(of: target, with: withString, options: NSString.CompareOptions.literal, range: nil)
    }
}

#5楼

A category that modifies an existing mutable String: 修改现有可变字符串的类别:

extension String
{
    mutating func replace(originalString:String, withString newString:String)
    {
        let replacedString = self.stringByReplacingOccurrencesOfString(originalString, withString: newString, options: nil, range: nil)
        self = replacedString
    }
}

Use: 采用:

name.replace(" ", withString: "+")

#6楼

If you don't want to use the Objective-C NSString methods, you can just use split and join : 如果您不想使用Objective-C NSString方法,则可以使用splitjoin

var string = "This is my string"
string = join("+", split(string, isSeparator: { $0 == " " }))

split(string, isSeparator: { $0 == " " }) returns an array of strings ( ["This", "is", "my", "string"] ). split(string, isSeparator: { $0 == " " })返回一个字符串数组( ["This", "is", "my", "string"] )。

join joins these elements with a + , resulting in the desired output: "This+is+my+string" . join+将这些元素连接起来,产生所需的输出: "This+is+my+string"

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值