Remove occurrence of Unicode character \u{ef} from String In Swift 3 - swift

Remove occurrence of Unicode character \u{ef} from String In Swift 3.
Example String:- "\u{ef}\n \n\u{ef}\n 🍏\n\u{ef}"
Thanks In advance.

Use the replacingOccurrences on your String:
let str = "\u{ef}\n \n\u{ef}\n 🍏\n\u{ef}".trimmingCharacters(in: .whitespaces)
let newStr = str.replacingOccurrences(of: "\u{ef}", with: "", options: NSString.CompareOptions.literal, range:nil)
print(newStr) // 🍏

Related

Swift Regex: Remove numbers embedded inside words in a String

Goal: Remove numbers embedded inside a string.
Example: let testString = "5What's9 wi3th this pro9ject I'm try905ing to build."
Desired Output: testString = "5What's9 with this project I'm trying to build"
What I've Tried:
let resultString = testString
.replacingOccurrences(of: "\\b[:digit:]\\b", with: "", options: .regularExpression)
// fails, returns string as is
let resultString = testString
.replacingOccurrences(of: "(\\d+)", with: "", options: .regularExpression)
// fails, returns all numbers removed from string.. close
let resultString = testString
.replacingOccurrences(of: "[0-9]", with: "", options: .regularExpression)
// removes all numbers from string.. close
How can we remove numbers that are inside of words only?
We can try doing a regex replacement on the following pattern:
(?<=\S)\d+(?=\S)
This matches only numbers surrounded on both sides by non whitespace characters. Updated code:
let resultString = testString
.replacingOccurrences(of: "(?<=\\S)\\d+(?=\\S)", with: "", options: .regularExpression)

Remove a line of characters in a String with Swift

I read many questions about removing characters from a string. But none of them resolved my issue.
I have this string:
"\"iconCls\":X.net.RM.getIcon(\"BulletWhite\")"
I want to replace this part:
"X.net.RM.getIcon(\"BulletWhite\")"
By this (double quotes in fact):
"\"\""
I use this code:
let dataString = "\"iconCls\":X.net.RM.getIcon(\"BulletWhite\")"
let newString = dataString?.replacingOccurrences(of: "X.net.RM.getIcon(\"BulletWhite\")" as String, with: "", options: .regularExpression, range: nil)
But it doesn't work. I can replace all characters until I want to replace strings containing parentheses.Any idea? Thanks!
You are passing the .regularExpression option but you are not actually using a regular expression.
Change:
.regularExpression
to:
[]
This gives the result you want:
let dataString = "\"iconCls\":X.net.RM.getIcon(\"BulletWhite\")"
let newString = dataString.replacingOccurrences(of: "X.net.RM.getIcon(\"BulletWhite\")" as String, with: "", options: [], range: nil)
Output:
"icnCls":
Even simpler:
let newString = dataString.replacingOccurrences(of: "X.net.RM.getIcon(\"BulletWhite\")" as String, with: "")
You don't need to use options or range for this.
let str = "\"iconCls\":X.net.RM.getIcon(\"BulletWhite\")"
let replace = "X.net.RM.getIcon(\"BulletWhite\")"
let replaceBy = "\"\""
let newString = str.replacingOccurrences(of: replace, with: replaceBy)

How can I substring this string?

how can I substring the next 2 characters of a string after a certian character. For example I have a strings str1 = "12:34" and other like str2 = "12:345. I want to get the next 2 characters after : the colons.
I want a same code that will work for str1 and str2.
Swift's substring is complicated:
let str = "12:345"
if let range = str.range(of: ":") {
let startIndex = str.index(range.lowerBound, offsetBy: 1)
let endIndex = str.index(startIndex, offsetBy: 2)
print(str[startIndex..<endIndex])
}
It is very easy to use str.index() method as shown in #MikeHenderson's answer, but an alternative to that, without using that method is iterating through the string's characters and creating a new string for holding the first two characters after the ":", like so:
var string1="12:458676"
var nr=0
var newString=""
for c in string1.characters{
if nr>0{
newString+=String(c)
nr-=1
}
if c==":" {nr=2}
}
print(newString) // prints 45
Hope this helps!
A possible solution is Regular Expression,
The pattern checks for a colon followed by two digits and captures the two digits:
let string = "12:34"
let pattern = ":(\\d{2})"
let regex = try! NSRegularExpression(pattern: pattern, options: [])
if let match = regex.firstMatch(in: string, range: NSRange(location: 0, length: string.characters.count)) {
print((string as NSString).substring(with: match.rangeAt(1)))
}

I need to remove this using encoding or decoding or replacement to remove \\U00b4 from string

I am getting I\\U00b4m in place of I'm. How to decode or replace this \\U00b4 from string ?
I\\U00b4m looking for the only one man in my life.
Easy way:
let aString = "This is my string"
let newString = aString.replacingOccurrences(of: "\U00b4", with: "", options: .literal, range: nil)
More info in here: Any way to replace characters on Swift String?

Swift replace full line in String

I have multiple lines in an String, for example:
let str = "Mieter: Hannes Tester \nVerwalter: Michael Karner \n"
Now i want to remove the whole sentence between "Mieter" and the line break. So the result should be:
let str = "Verwalter: Michael Karner \n"
I could check with Regex, but i am only able to get the string between 2 words. For example with:
if let match = str.rangeOfString("(?<=Mieter)[^\n]+", options: .RegularExpressionSearch) {
print(str.substringWithRange(match)) // between
}
But how can i replace a whole line?
Edit:
It is not working anymore when its between a string:
let str = "Test \n Mieter: \n Hausverwalter: \n Firma: \n"
str.stringByReplacingOccurrencesOfString("^Mieter[^\n]+\\s", withString: "", options: .RegularExpressionSearch, range: nil)
// so "Mieter \n" is not being replaced.
Use replacingOccurrencesOf
let str = "Mieter: Hannes Tester \nVerwalter: Michael Karner \n"
str.replacingOccurrences(of: "^Mieter[^\n]+\\s", with: "", options: .regularExpression)
This is Swift 3 code
Edit:
If the substring is not at the beginning of the string remove the leading caret (^).