Arabic Swift String isn't empty after removing all characters - ios

I have this code to replace part of a string and remove white spaces:
let str = "باب ‏".replacingOccurrences(of: "باب", with: "").trimmingCharacters(in: .whitespacesAndNewlines)
print(str.count) /// gives 1 why not 0
But it gives me 1 always while it should be 0. Why?

If it's RTL Mark (and it probably is), that's "\u{200F}" in Swift. If you want to trim it with the whitespace, you'd just add it to your set. That'd be something like:
.trimmingCharacters(in: whitespacesAndNewlines
.union(CharacterSet(charactersIn: "\u{200f}")))
You can also just replace that directly:
.replacingOccurrences(of: "\u{200f}باب", with: "")
Keep in mind the layout rules, since sometimes bidirectional literal strings like this can get confusing in the editor. You may want to separate the Arabic like:
let bab = "باب"
let rtl = "\u{200f}"
string.replacingOccurrences(of: rtl + bab, with: "")

Let's look at the content of your original string:
func hexCharactersArray(_ string: String) -> String {
string.unicodeScalars.map { String(format: "0x%X", $0.value)}.joined(separator: ",")
}
let originalString = "باب ‏"
print(hexCharactersArray(originalString))
The result is [0x628,0x627,0x628,0x20,0x200F]
0x628 - arabic letter beh
0x627 - arabic letter alef
0x628 - arabic letter beh
0x20 - space
0x200F - right-to-left mark
The first three are letters, then some whitespace, but 0x200F is a unicode character in the category of control characters. It's not a letter and it's not whitespace.
When you do:
let replacedString = originalString.replacingOccurrences(of: "باب", with: "").trimmingCharacters(in: .whitespacesAndNewlines)
print(hexCharactersArray(replacedString))
you get [0x200F]
Because you've replaced the letters and trimmed out the whitespace, but you've left behind a control character.
If you want to trim that out too, use:
let replacedString = originalString.replacingOccurrences(of: "باب", with: "").trimmingCharacters(in: .whitespacesAndNewlines.union(.controlCharacters))

Related

Removing Symbols and Spaces from a Phone Number in Swift

I have a phone number variable that is assigned a value.
let phone: String = "+1 (512) 477-2900"
I want to remove all the spaces and characters including "(", "+", ")" and "-" from the phone number. How can I do that?
// remove the spaces
let formattedPhone = phone.replacingOccurrences(of: " ", with: "")
I don't know how to remove other characters as mentioned earlier.
I assume you want to remove all the characters in the string other than numbers.
let updatedPhone = phone.filter{$0.isNumber}
print(updatedPhone) // 15124772900

Cut a String from start position to end position with swift 3

I have Strings with the form string \ string example
"some sting with random length\233"
I want to deletes the last \ and get the value after it, so the result will be
"some sting with random length"
"233"
I tried this code but it's not working
let regex = try! NSRegularExpression(pattern: "\\\s*(\\S[^,]*)$")
if let match = regex.firstMatch(in: string, range: string.nsRange) {
let result = string.substring(with: match.rangeAt(1))
}
You did not correctly adapt the pattern from How to get substring after last occurrence of character in string: Swift IOS to your case. Both instances of the comma must be replaced by a backslash,
and that must be "double-escaped":
let regex = try! NSRegularExpression(pattern: "\\\\\\s*(\\S[^\\\\]*)$")
(once be interpreted as a literal backslash in the regex pattern, and
once more in the definition of a Swift string literal).
However, a simpler solution is to find the last occurrence of the
backslash and extract the suffix from that position:
let string = "some sting with random length\\233"
let separator = "\\" // A single(!) backslash
if let r = string.range(of: separator, options: .backwards) {
let prefix = string.substring(to: r.lowerBound)
let suffix = string.substring(from: r.upperBound)
print(prefix) // some sting with random length
print(suffix) // 233
}
Update for Swift 4:
if let r = string.range(of: separator, options: .backwards) {
let prefix = string[..<r.lowerBound]
let suffix = string[r.upperBound...]
print(prefix) // some sting with random length
print(suffix) // 233
}
prefix and suffix are a String.SubSequence, which can be used
in many places instead of a String. If necessary, create a real
string:
let prefix = String(string[..<r.lowerBound])
let suffix = String(string[r.upperBound...])
You could do this with regex, but I think this solution is better:
yourString.components(separatedBy: "\\").last!
It splits the string with \ as the separator and gets the last split.

Remove U\0000fffc unicode scalar from string

I receive an NSAttributedString that contains a NSTextAttachment. I want to remove that attachment, and it looks like it is represented as "\u{ef}" in the string. Printing the unicode scalars of such string, it also seems that unicode scalar for the "\u{ef}" is U\0000fffc.
I tried to do this:
noAttachmentsText = text.replacingOccurrences(of: "\u{ef}", with: "")
with no success, so I'm trying by comparing unicode scalars:
var scalars = Array(text.unicodeScalars)
for scalar in scalars {
// compare `scalar` to `U\0000fffc`
}
but I'm not able either to succeed in the comparison.
How could I do this?
But this code works for me from How do I remove "\U0000fffc" from a string in Swift?
let original = "First part \u{ef} Last part"
let originalRange = Range<String.Index>(start: original.startIndex, end: original.endIndex)
let target = original.stringByReplacingOccurrencesOfString("\u{ef}", withString: "", options: NSStringCompareOptions.LiteralSearch, range: originalRange)
print(target)
Output :
"First part ï Last part"
to
First part Last part
U can use similar code for swift 3 just replace unicode using replacingOccurrences option for exapmle :
func stringTocleanup(str: String) -> String {
var result = str
result = result.replacingOccurrences(of: "\"", with: "\"")
.replacingOccurrences(of: "\u{10}", with: "")
return result
}

Strip leading, trailing and more than 1 space from String

I have string:
Simple text with spaces
I need regular expression which select:
leading
trailing
more than 1 space
Example:
_ - space
_Simple text __with ___spaces_
My 2ct:
let text = " Simple text with spaces "
let pattern = "^\\s+|\\s+$|\\s+(?=\\s)"
let trimmed = text.stringByReplacingOccurrencesOfString(pattern, withString: "", options: .RegularExpressionSearch)
println(">\(trimmed)<") // >Simple text with spaces<
^\s+ and \s+$ match one or more white space characters at the start/end of the string.
The tricky part is the \s+(?=\s) pattern, which matches one or more
white space characters followed by another white space character which itself is not considered
part of the match (a "look-ahead assertion").
Generally, \s matches all white-space characters such as the space character itself, horizontal tabulation, newline, carriage-return, linefeed or formfeed. If
you want to remove only the (repeated) space characters then replace the pattern by
let pattern = "^ +| +$| +(?= )"
You can keep the regular expression simple by doing the leading/trailing part as a second stage:
let singlySpaced = " Simple text with spaces "
.stringByReplacingOccurrencesOfString("\\s+", withString: " ", options: .RegularExpressionSearch)
.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
(assuming you want to strip all kinds of whitespace – you can adjust it down to just do only spaces fairly easily)
There are more complex regexprs that will do it in one shot, but personally I prefer the two-step version over obfuscation (and as #MartinR mentions, performance is very similar between the two, given a trim is a very lightweight operation vs a slower more-complex regex - so it’s really down to which you prefer the look of).
This ought to clean your strings up:
var string : NSString = " hello world. "
while string.rangeOfString(" ").location != NSNotFound { //note the use of two spaces
string = string.stringByReplacingOccurrencesOfString(" ", withString: " ")
}
println(string)
string = string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
println(string)
Some good answers have been provided, but if you want a regex the following should work:
^ |(?<= ) +| $
The | indicates alternatives, the ^ is the beginning of a string, the $ indicates the end of the string. So this matches beginning of the string followed by a space OR one or more spaces preceded by a space OR a space at the end of the string.
Following will remove all the spaces
NSString *spaces =#"hi how are you ";
NSString *withoutSpace= [spaces stringByReplacingOccurrencesOfString:#" "
withString:#" "];
withoutSpace = [withoutSpace stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]];
this function will leave one space between words
let accountNameStr = " test test "
let trimmed = accountNameStr.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
let textWithoutSpace = trimmed.trimmingCharacters(in: .whitespaces)
Output : test test

Swift - Remove " character from string

I have a string which is "Optional("5")". I need to remove the "" surrounding the 5. I have removed the 'Optional' by doing:
text2 = text2.stringByReplacingOccurrencesOfString("Optional(", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
I am having difficulties removing the " characters as they designate the end of a string in the code.
Swift uses backslash to escape double quotes. Here is the list of escaped special characters in Swift:
\0 (null character)
\\ (backslash)
\t (horizontal tab)
\n (line feed)
\r (carriage return)
\" (double quote)
\' (single quote)
This should work:
text2 = text2.replacingOccurrences(of: "\\", with: "", options: NSString.CompareOptions.literal, range: nil)
Swift 3 and Swift 4:
text2 = text2.textureName.replacingOccurrences(of: "\"", with: "", options: NSString.CompareOptions.literal, range:nil)
Latest documents updated to Swift 3.0.1 have:
Null Character (\0)
Backslash (\\)
Horizontal Tab (\t)
Line Feed (\n)
Carriage Return (\r)
Double Quote (\")
Single Quote (\')
Unicode scalar (\u{n}), where n is between one and eight hexadecimal digits
If you need more details you can take a look to the official docs here
Here is the swift 3 updated answer
var editedText = myLabel.text?.replacingOccurrences(of: "\"", with: "")
Null Character (\0)
Backslash (\\)
Horizontal Tab (\t)
Line Feed (\n)
Carriage Return (\r)
Double Quote (\")
Single Quote (\')
Unicode scalar (\u{n})
To remove the optional you only should do this
println("\(text2!)")
cause if you dont use "!" it takes the optional value of text2
And to remove "" from 5 you have to convert it to NSInteger or NSNumber easy peasy. It has "" cause its an string.
Replacing for Removing is not quite logical.
String.filter allows to iterate a string char by char and keep only true assertion.
Swift 4 & 5
var aString = "Optional(\"5\")"
aString = aString.filter { $0 != "\"" }
> Optional(5)
Or to extend
var aString = "Optional(\"5\")"
let filteredChars = "\"\n\t"
aString = aString.filter { filteredChars.range(of: String($0)) == nil }
> Optional(5)
I've eventually got this to work in the playground, having multiple characters I'm trying to remove from a string:
var otherstring = "lat\" : 40.7127837,\n"
var new = otherstring.stringByTrimmingCharactersInSet(NSCharacterSet.init(charactersInString: "la t, \n \" ':"))
count(new) //result = 10
println(new)
//yielding what I'm after just the numeric portion 40.7127837
If you want to remove more characters for example "a", "A", "b", "B", "c", "C" from string you can do it this way:
someString = someString.replacingOccurrences(of: "[abc]", with: "", options: [.regularExpression, .caseInsensitive])
As Martin R says, your string "Optional("5")" looks like you did something wrong.
dasblinkenlight answers you so it is fine, but for future readers, I will try to add alternative code as:
if let realString = yourOriginalString {
text2 = realString
} else {
text2 = ""
}
text2 in your example looks like String and it is maybe already set to "" but it looks like you have an yourOriginalString of type Optional(String) somewhere that it wasn't cast or use correctly.
I hope this can help some reader.
Swift 5 (working). Only 1 line code.
For removing single / multiple characters.
trimmingCharacters(in: CharacterSet)
In action:
var yourString:String = "(\"This Is: Your String\")"
yourString = yourString.trimmingCharacters(in: ["("," ",":","\"",")"])
print(yourString)
Output:
ThisIsYourString
You are entering a Set that contains characters you're required to trim.
Let's say you have a string:
var string = "potatoes + carrots"
And you want to replace the word "potatoes" in that string with "tomatoes"
string = string.replacingOccurrences(of: "potatoes", with: "tomatoes", options: NSString.CompareOptions.literal, range: nil)
If you print your string, it will now be: "tomatoes + carrots"
If you want to remove the word potatoes from the sting altogether, you can use:
string = string.replacingOccurrences(of: "potatoes", with: "", options: NSString.CompareOptions.literal, range: nil)
If you want to use some other characters in your sting, use:
Null Character (\0)
Backslash (\)
Horizontal Tab (\t)
Line Feed (\n)
Carriage Return (\r)
Double Quote (\")
Single Quote (\')
Example:
string = string.replacingOccurrences(of: "potatoes", with: "dog\'s toys", options: NSString.CompareOptions.literal, range: nil)
Output: "dog's toys + carrots"
If you are getting the output Optional(5) when trying to print the value of 5 in an optional Int or String, you should unwrap the value first:
if value != nil
{ print(value)
}
or you can use this:
if let value = text {
print(value)
}
or in simple just 1 line answer:
print(value ?? "")
The last line will check if variable 'value' has any value assigned to it, if not it will print empty string
You've instantiated text2 as an Optional (e.g. var text2: String?).
This is why you receive Optional("5") in your string.
take away the ? and replace with:
var text2: String = ""
If you are getting the output Optional(5) when trying to print the value of 5 in an optional Int or String, you should unwrap the value first:
if let value = text {
print(value)
}
Now you've got the value without the "Optional" string that Swift adds when the value is not unwrapped before.

Resources