Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 获取一个特定字符的字符串_Swift_String_Character - Fatal编程技术网

Swift 获取一个特定字符的字符串

Swift 获取一个特定字符的字符串,swift,string,character,Swift,String,Character,我想删除@符号后面的每个字母 结果应该是 var hello = "hello, how are you?" var hello2 = "hello, how are you @tom?" 更新 因为我想用它链接多个用户并用正确的名称替换@sign后面的空格,所以我总是需要引用最新出现的@sign来替换它 var hello2 = "hello, how are you @tom?" -> hello2.trimmed() print(hello2.trimmed()) ->

我想删除@符号后面的每个字母

结果应该是

var hello = "hello, how are you?"

var hello2 = "hello, how are you @tom?"
更新 因为我想用它链接多个用户并用正确的名称替换@sign后面的空格,所以我总是需要引用最新出现的@sign来替换它

var hello2 = "hello, how are you @tom?"
->
hello2.trimmed() 

print(hello2.trimmed())

-> "hello, how are you"
示例最终版本应该是什么样子

出发

var text=“你好@tom@mark@mathias”


我希望始终获得文本中最新@符号的索引

您需要找到“@”的范围,然后使用它创建索引前的子字符串

text3 = "hey i love you @Tom @Marcus @Peter"
考虑 请注意,按照您描述的逻辑并使用您提供的输入文本,输出字符串将有一个空格作为最后一个字符

还请注意,如果输入文本中出现多个
@
,则将使用第一个匹配项


最后索引[更新] 我添加这个新的部分是为了回答您在评论中提出的问题

如果你有这样的文字

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"

您需要找到“@”的范围,然后使用它创建索引之前的子字符串

text3 = "hey i love you @Tom @Marcus @Peter"
考虑 请注意,按照您描述的逻辑并使用您提供的输入文本,输出字符串将有一个空格作为最后一个字符

还请注意,如果输入文本中出现多个
@
,则将使用第一个匹配项


最后索引[更新] 我添加这个新的部分是为了回答您在评论中提出的问题

如果你有这样的文字

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"
可以为字符串或字符集合提供一个范围,告诉它要获取哪些字符。括号内的表达式

    ??
。。
可以为字符串或字符集合提供一个范围,告诉它要获取哪些字符。括号内的表达式

    ??

。在@appzyorlife answer上展开后,以下内容还将在删除@符号后的所有内容后删除空白字符

    text[..<trimSpot]
更新:

为了在字符串中找到最后出现的
@
符号并将其删除,我将采用以下方法:

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"
或者查看@appzyorlife answer,使用
range(of:options:range:locale:)
而不是字面上颠倒字符

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"
}
作为额外的奖励,以下是我将如何从最后一个开始删除每个
@
,然后继续:

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"
}

在@appzyorlife answer上展开后,以下内容还将删除@符号后的所有空格字符

    text[..<trimSpot]
更新:

为了在字符串中找到最后出现的
@
符号并将其删除,我将采用以下方法:

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"
或者查看@appzyorlife answer,使用
range(of:options:range:locale:)
而不是字面上颠倒字符

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"
}
作为额外的奖励,以下是我将如何从最后一个开始删除每个
@
,然后继续:

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"
}

试试正则表达式,它们更安全(如果你知道你在做什么…)

这段代码完全删除了您需要的内容,并将字符串后的字符保留为空白,这样您就可以看到,和?仍然在那里。:)

编辑:您在对此答案的评论中提出的问题:

  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?"

    let deletedStringsAfterAtSign = hello2.replacingOccurrences(of: "@\\w+", with: "", options: .regularExpression, range: nil)

    print(deletedStringsAfterAtSign)
    //prints "hello, how are you ,   ?"


谢谢,我会在我回到我的项目后立即尝试,这很有意义,而且空的空间也很好,谢谢:)我用它来链接用户,所以如果我想链接多个用户,这有用吗?我将在我的代码中实现一个示例。如果你提到的只有一个@,那么这个示例会起作用,但我需要它来查找文本中的最后一个@符号。如果我需要最新的@符号,那么解决方案是什么?@Zash_uuu请提供一个示例输入和预期输出。Hanks我将在我回到项目后立即尝试,这很有意义,而且空的空间也很好,谢谢:)我用它来链接用户,所以如果我想链接多个,这行吗?我将在我的代码中实现一个示例。如果您提到的只有一个@,那么这个示例会起作用,但我需要它来查找文本中的最后一个@符号。如果我需要最新出现的@符号,那么解决方案是什么?@Zash_uuuu请提供一个示例输入和预期的outputindex(of:)不是我看到的字符串数据类型的有效方法。我正在使用Swift 3.1,所以它没有给我这个选项。它是Swift 4对不起,我总是使用Swift 3,因为我没有得到回复^^谢谢你提供这个代码片段,它可能会提供一些即时帮助。一个恰当的解释将通过说明为什么这是一个很好的解决问题的方法来体现它的教育价值,并将使它对未来有类似但不完全相同问题的读者更有用。请编辑您的答案以添加解释,并说明适用的限制和假设。这并不能解决我的问题,因为我只想用cell.namelab替换最新@符号后面的字符串。当我将@写入并按下带有标签tomas的单元格时,它会将文本更改为@tomas。然后,我应该能够继续链接更多的用户@lo,并按下名为Lola的单元格,它应该是@Lola->“@tomas@Lola”。(没有空格,但我无法在堆栈上@sign和username之间没有空格发送)index(of:)不是字符串数据类型的有效方法。我在Swift 3.1中打基础,所以它没有给我这个选择