var hello = "hello, how are you?"
var hello2 = "hello, how are you @tom?"
我想删除@符号后面的每个字母。
结果应该是
var hello2 = "hello, how are you @tom?"
->
hello2.trimmed()
print(hello2.trimmed())
-> "hello, how are you"
更新 由于我想用它链接多个用户并用正确的名称替换@sign后面的空格,我总是需要引用最新出现的@sign来替换它。
text3 = "hey i love you @Tom @Marcus @Peter"
示例最终版本应该是什么样的
开始
var text =" hello @tom @mark @ mathias"
我希望始终在文本
中获取最新@符号的索引答案 0 :(得分:2)
扩展@appzYourLife的答案,在删除@符号后的所有内容后,以下内容也将删除空白字符。
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"
<强>更新强>
为了回应在字符串中找到@
符号的最后一次出现并将其删除,以下是我将如何处理它:
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:)
而不是字面颠倒字符
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"
}
作为一个额外的好处,以下是我将如何从最后一个开始删除每个@
并继续前进:
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"
}
答案 1 :(得分:1)
你需要找到“@”的范围,然后用它来创建一个直到索引的子字符串。
import Foundation
let text = "hello, how are you @tom?"
if let range = text.range(of: "@") {
let result = text.substring(to: range.lowerBound)
print(result) // "hello, how are you "
}
请注意,按照您描述的逻辑并使用您提供的输入文本,输出字符串将有一个空格作为最后一个字符
另请注意,如果输入文本中有多个
@
,则会使用第一个匹配项。
我正在添加这个新部分来回答您在评论中发布的问题。
如果你有这样的文字
let text = "hello @tom @mark @mathias"
并且你想要你可以编写的"@"
最后个并发的索引
if let index = text.range(of: "@", options: .backwards)?.lowerBound {
print(index)
}
答案 2 :(得分:1)
let text = "hello, how are you @tom?"
let trimSpot = text.index(of: "@") ?? text.endIndex
let trimmed = text[..<trimSpot]
由于字符串是Character类型的集合,因此可以这样访问它。第二行找到@符号的索引并将其值赋给trimSpot,但如果不存在,则通过使用nil合并运算符来分配字符串的endIndex
??
字符串或字符集合可以提供一个范围,告诉它要获取哪些字符。括号内的表达式
..<trimSpot
是从0到trimSpot-1的范围。所以,
text[..<trimSpot]
返回一个Substring类型的实例,它指向原始的String实例。
答案 3 :(得分:1)
尝试正则表达式,它们更安全(如果你知道你在做什么......)
let hello2 = "hello, how are you @tom, @my @next @victim?"
let deletedStringsAfterAtSign = hello2.replacingOccurrences(of: "@\\w+", with: "", options: .regularExpression, range: nil)
print(deletedStringsAfterAtSign)
//prints "hello, how are you , ?"
这段代码完全删除了你需要的东西,并在字符串清除后留下字符,这样你就可以看到了,和?还在那里。 :)
编辑:您在评论中对此答案提出的问题:
let hello2 = "hello, how are you @tom, @my @next @victim?"
if let elementIwannaAfterEveryAtSign = hello2.components(separatedBy: " @").last
{
let deletedStringsAfterAtSign = hello2.replacingOccurrences(of: "@\\w+", with: elementIwannaAfterEveryAtSign, options: .regularExpression, range: nil)
print(deletedStringsAfterAtSign)
//prints hello, how are you victim?, victim? victim? victim??
}