问题
var hello = "hello, how are you?"
var hello2 = "hello, how are you @tom?"
i want to delete every letter behind the @ sign.
result should be
var hello2 = "hello, how are you @tom?"
->
hello2.trimmed()
print(hello2.trimmed())
-> "hello, how are you"
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
to start off
var text = "hello @tom @mark @mathias"
i want to always get the index of the latest @ sign in the text
回答1:
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"
UPDATE:
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"
}
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"
}
回答2:
let text = "hello, how are you @tom?"
let trimSpot = text.index(of: "@") ?? text.endIndex
let trimmed = text[..<trimSpot]
Since a string is a collection of Character type, it can be accessed as such. The second line finds the index of the @ sign and assigns its value to trimSpot, but if it is not there, the endIndex of the string is assigned through the use of the nil coalescing operator
??
The string, or collection of Characters, can be provided a range that will tell it what characters to get. The expression inside of the brackets,
..<trimSpot
is a range from 0 to trimSpot-1. So,
text[..<trimSpot]
returns an instance of type Substring, which points at the original String instance.
回答3:
You need to find the range of the "@" and then use it to create a substring up to the index before.
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 "
}
Considerations
Please note that, following the logic you described and using the input text you provided, the output string will have a blank space as last character
Also note that if multiple
@are presente in the input text, then the first occurrence will be used.
Last index [Update]
I am adding this new section to answer the question you posted in the comments.
If you have a text like this
let text = "hello @tom @mark @mathias"
and you want the index of the last occurrency of "@" you can write
if let index = text.range(of: "@", options: .backwards)?.lowerBound {
print(index)
}
回答4:
Try regular expressions, they are much safer (if you know what you are doing...)
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 , ?"
And this code removes exactly what you need and leaves the characters after the strings clear, so you can see the , and ? still being there. :)
EDIT: what you asked in comments to this answer:
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??
}
来源:https://stackoverflow.com/questions/45067919/get-the-string-up-to-a-specific-character