获取字符串到特定字符 [英] Get the string up to a specific character
问题描述
var hello = "hello, how are you?"
var hello2 = "hello, how are you @tom?"
我想删除@符号后面的每个字母。
i want to delete every letter behind the @ sign.
结果应该是
var hello2 = "hello, how are you @tom?"
->
hello2.trimmed()
print(hello2.trimmed())
-> "hello, how are you"
更新
想要使用它链接多个用户并用正确的名称替换@sign后面的空格,我总是需要引用最新出现的@sign来替换它。
Update As i want to use it to link multiple users and replace the space behind @sign with the correct name, I always need the reference to the latest occurrence of the @sign to replace it.
text3 = "hey i love you @Tom @Marcus @Peter"
示例最终版本应该是什么样子
Example what the final version should look like
开始
var text = hello @tom @mark @mathias
var text = "hello @tom @mark @mathias"
我想始终获取文本中最新@符号的索引
i want to always get the index of the latest @ sign in the text
推荐答案
在@appzYourLife答案上进行扩展,在删除@符号后的所有内容后,以下内容也将修剪空白字符。
Expanding on @appzYourLife answer, the following will also trim off the whitespace characters after removing everything after the @ symbol.
import Foundation
var str = "hello, how are you @tom"
if str.contains("@") {
let endIndex = str.range(of: "@")!.lowerBound
str = str.substring(to: endIndex).trimmingCharacters(in: .whitespacesAndNewlines)
}
print(str) // Output - "hello, how are you"
更新:
为响应 @的最后一次出现
删除字符串中的符号,这是我的处理方法:
In response to finding the last occurance of the @
symbol in the string and removing it, here is how I would approach it:
var str = "hello, how are you @tom @tim?"
if str.contains("@") {
//Reverse the string
var reversedStr = String(str.characters.reversed())
//Find the first (last) occurance of @
let endIndex = reversedStr.range(of: "@")!.upperBound
//Get the string up to and after the @ symbol
let newStr = reversedStr.substring(from: endIndex).trimmingCharacters(in: .whitespacesAndNewlines)
//Store the new string over the original
str = String(newStr.characters.reversed())
//str = "hello, how are you @tom"
}
在@appzYourLife答案中使用 range(of:options:range:locale:)
而不是从字面上反转字符
Or looking at @appzYourLife answer use range(of:options:range:locale:)
instead of literally reversing the characters
var str = "hello, how are you @tom @tim?"
if str.contains("@") {
//Find the last occurrence of @
let endIndex = str.range(of: "@", options: .backwards, range: nil, locale: nil)!.lowerBound
//Get the string up to and after the @ symbol
let newStr = str.substring(from: endIndex).trimmingCharacters(in: .whitespacesAndNewlines)
//Store the new string over the original
str = newStr
//str = "hello, how are you @tom"
}
作为额外的奖励,这是我将删除每个 @
的方式从最后一个开始,然后向前进行:
As an added bonus, here is how I would approach removing every @
starting with the last and working forward:
var str = "hello, how are you @tom and @tim?"
if str.contains("@") {
while str.contains("@") {
//Reverse the string
var reversedStr = String(str.characters.reversed())
//Find the first (last) occurance of @
let endIndex = reversedStr.range(of: "@")!.upperBound
//Get the string up to and after the @ symbol
let newStr = reversedStr.substring(from: endIndex).trimmingCharacters(in: .whitespacesAndNewlines)
//Store the new string over the original
str = String(newStr.characters.reversed())
}
//after while loop, str = "hello, how are you"
}
这篇关于获取字符串到特定字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!