Split text into array while maintaining the punctuation in Swift - ios

I want to split the text into an array, maintaining the punctuation separated by the rest of the words, so a string like:
Hello, I am Albert Einstein.
should turn into an array like this:
["Hello", ",", "I", "am", "Albert", "Einstein", "."]
I have tried with sting.components(separatedBy: CharacterSet.init(charactersIn: " ,;;:")) but this method deletes all punctuations, and returns an array like this:
["Hello", "I", "am", "Albert", "Einstein"]
So, how can I get an array like my first example?

It's not beautiful as solution but you can try with:
var str = "Hello, I am Albert Einstein."
var list = [String]()
var currentSubString = "";
//enumerate to get all characters including ".", ",", ";", " "
str.enumerateSubstrings(in: str.startIndex..<str.endIndex, options: String.EnumerationOptions.byComposedCharacterSequences) { (substring, substringRange, enclosingRange, value) in
if let _subString = substring {
if (!currentSubString.isEmpty &&
(_subString.compare(" ") == .orderedSame
|| _subString.compare(",") == .orderedSame
|| _subString.compare(".") == .orderedSame
|| _subString.compare(";") == .orderedSame
)
) {
//create word if see any of those character and currentSubString is not empty
list.append(currentSubString)
currentSubString = _subString.trimmingCharacters(in: CharacterSet.whitespaces )
} else {
//add to current sub string if current character is not space.
if (_subString.compare(" ") != .orderedSame) {
currentSubString += _subString
}
}
}
}
//last word
if (!currentSubString.isEmpty) {
list.append(currentSubString)
}
In Swift3:
var str = "Hello, I am Albert Einstein."
var list = [String]()
var currentSubString = "";
//enumerate to get all characters including ".", ",", ";", " "
str.enumerateSubstrings(in: str.startIndex..<str.endIndex, options: String.EnumerationOptions.byComposedCharacterSequences) { (substring, substringRange, enclosingRange, value) in
if let _subString = substring {
if (!currentSubString.isEmpty &&
(_subString.compare(" ") == .orderedSame
|| _subString.compare(",") == .orderedSame
|| _subString.compare(".") == .orderedSame
|| _subString.compare(";") == .orderedSame
)
) {
//create word if see any of those character and currentSubString is not empty
list.append(currentSubString)
currentSubString = _subString.trimmingCharacters(in: CharacterSet.whitespaces )
} else {
//add to current sub string if current character is not space.
if (_subString.compare(" ") != .orderedSame) {
currentSubString += _subString
}
}
}
}
//last word
if (!currentSubString.isEmpty) {
list.append(currentSubString)
}
The idea is to loop for all character and create word in same time. A word is a group of consecutive character that is not , ,, . or ;. So, during the creation of word in loop, we finish the current word if we see one of those character, and the current word in construction is not empty.
To break down steps with your input:
get H (not space nor other terminal character)
-> currentSubString = "H"
get e (not space nor other terminal character)
-> currentSubString = "He"
get l (not space nor other terminal character)
-> currentSubString = "Hel"
get l (not space nor other terminal character)
-> currentSubString = "Hell"
get o (not space nor other terminal character)
-> currentSubString = "Hello"
get . (is terminal character)
-> as currentSubString is not empty, add to list and restart the construction for next word, then list = ["Hello"]
-> currentSubString = "." (the reason that I used trimming is just to remove if I get this character. but for other terminal character, we have to keep for next word.
get (is space character)
-> as currentSubString is not empty, add to listand restart the construction -> list = ["Hello", "."]
-> currentSubString = "" (trimmed).
... and so on.

To explain from my comment... Think of regular expressions as a way to nicely find patterns within Strings. In your case, the pattern is words (groups of letters) with other possible symbols (punctuation marks) in between.
Take the regex in my comment (which I've expanded a bit here), for example: ([,\.\:\"])*([A-Za-z0-9\']*)([,\.\:\"])*
In there, we have 3 groups. The first searches for any symbols (such as a leading quotation mark). The second is searching for letters, numbers, and an apostrophe (because people like to concatenate words, like "I'm"). and the third group searches for any trailing punctuation marks.
Edit to note: groups in the above are denoted by parentheses ( and ), while the [ and ] brackets denote acceptable characters for a search. So, for example, [A-Z] says that all upper case letters from A-Z are acceptable. [A-Za-z] lets you get both upper and lower, while [A-Za-z0-9] includes all letters and numbers from 0-9. Granted, there are shorthand versions to writing this, but those you'll discover down the road.
So now we have a way to separate all the words and punctuation marks, now you need to actually use it, doing something along the lines of:
func find(value: NSString) throws -> [NSString] {
let regex = try NSRegularExpression(pattern: "([,\\.\\:\\\"])*([A-Za-z0-9\\']*)([,\\.\\:\\\"])*") // Notice you have to escape the values in code
let results = regex.matches(in: value, range: NSRange(location: 0, length: nsString.length))
return results.map({ value.substring(with: $0.range) }).filter({ $0 != nil })
}
That should give you each non-nil group found within the String value you supply to the method.
Granted, that last filter method may not be necessary, but I'm not familiar enough with how Swift handles regex to know for sure.
But that should definitely point you in the right direction...
Cheers~

Related

How to remove special character from String in swift iOS

I need get value without special character from string
I tried this code but remove special character and letter
example :
var str = ".34!44fgf)(gg#$qwe3"
str.components(separatedBy: CharacterSet.decimalDigits.inverted)//result => 34443
i am want results the following without special character => "3444fgfggqwe3"
Please Advise
You can filter all characters that are letter or digits:
let result = str.filter { $0.isLetter || "0"..."9" ~= $0 }
print(result) // "3444fgfggqwe3"
If you would like to restrict the letters to only lowercase letters from "a" to "z"
"a"..."z" ~= $0
or "A" to "Z"
"A"..."Z" ~= $0
Actually result is a huge array with a lot of empty strings.
This is another approach with Regular Expression
var str = ".34!44fgf)(gg#$qwe3"
str = str.replacingOccurrences(of: "[^[0-9a-zA-z]]", with: "", options: .regularExpression)

backspace not work in outside of regex in swift

I use this method for patterning the phone number in UITextField at the .editingChange event
But the delete key only removes the numbers
extension String{
func applyPatternOnNumbers(pattern: String) -> String {
let replacmentCharacter: Character = "#"
let pureNumber = self.replacingOccurrences( of: "[^۰-۹0-9]", with: "", options: .regularExpression)
var result = ""
var pureNumberIndex = pureNumber.startIndex
for patternCharacter in pattern {
if patternCharacter == replacmentCharacter {
guard pureNumberIndex < pureNumber.endIndex else { return result }
result.append(pureNumber[pureNumberIndex])
pureNumber.formIndex(after: &pureNumberIndex)
} else {
result.append(patternCharacter)
}
}
return result
}
}
use at the editingChange event
let pattern = "+# (###) ###-####"
let mobile = textField.text.substring(to: pattern.count-1)
textfield.text = mobile.applyPatternOnNumbers(pattern: pattern)
// print(textfield.text) +1 (800) 666-8888
the problem is space & - , ( , ) chars can not to be removed
The RegEx you are trying is to not consider digits only:
[^۰-۹0-9]
I'm not sure, but you may change it to:
[^۰-۹0-9\s-\(\)]
and it may work. You might just add a \ before your special chars inside [] and you can any other chars into it that you do not need to be replaced.
Or you may simplify it to
[^\d\s-\(\)]
and it might work.
Method 2
You may use this RegEx which is an exact match to the phone number format you are having:
\+\d+\s\(\d{3}\)\s\d{3}-\d{4}
You may remove the first +, if it is unnecessary
\d+\s\(\d{3}\)\s\d{3}-\d{4}

Parsing & contracting Russian full names

I have several text fields used to enter full name and short name, among other data. My task is:
Check if entered full name matches the standard Russian Cyrillic full name pattern:
Иванов Иван Иванович (three capitalized Cyrillic strings separated by spaces)
If it matches, create another string by auto-contracting full name according to pattern below and enter it to the corresponding text field:
Иванов И.И. (first string, space, first character of the second string, dot, first character of the third string, dot)
If it doesn't match, do nothing.
Currently I use the following code:
let fullNameArray = fullNameField.text!.characters.split{$0 == " "}.map(String.init)
if fullNameArray.count == 3 {
if fullNameArray[0] == fullNameArray[0].capitalizedString && fullNameArray[1] == fullNameArray[1].capitalizedString && fullNameArray[2] == fullNameArray[2].capitalizedString {
shortNameField.text = "\(fullNameArray[0]) \(fullNameArray[1].characters.first!).\(fullNameArray[2].characters.first!)."
}
}
How can I improve it? Maybe regular expressions could help me? If so, could you post some sample code?
My current solution:
do {
let regex = try NSRegularExpression(pattern: "^\\p{Lu}\\p{Ll}+\\s\\p{Lu}\\p{Ll}+\\s\\p{Lu}\\p{Ll}+$", options: .AnchorsMatchLines)
if regex.firstMatchInString(fullNameField.text!, options: [], range: NSMakeRange(0, fullNameField.text!.characters.count)) != nil {
let fullNameArray = fullNameField.text!.characters.split(" ").map(String.init)
shortNameField.text = "\(fullNameArray[0]) \(fullNameArray[1].characters.first!).\(fullNameArray[2].characters.first!)."
}
else {
shortNameField.text = ""
}
} catch let error as NSError {
print(error.localizedDescription)
}
Processes my full name pattern correctly.

Check if string is 3 chars and 3 number in Swift

I'm trying to create a function that validate my string if it is using this format
ABC123
First three characters should be letters and the other 3 should be numbers
I have no idea on how to start
Thanks
You can do it with a regular expression match on strings, like this:
let str = "ABC123"
let optRange = str.rangeOfString("^[A-Za-z]{3}\\d{3}$", options: .RegularExpressionSearch)
if let range = optRange {
println("Matched")
} else {
println("Not matched")
}
Regex above requires that the match occupied the whole string (the ^ and $ anchors at both ends), has three letters [A-Za-z]{3} and three digits \\d{3}.
You can also use it as an extension if you would like to:
extension String {
var match: Bool {
return rangeOfString("^[A-Za-z]{3}\\d{3}$", options: .RegularExpressionSearch) != nil
}
}
"ABC123".match // true

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