How to remove Unicode U+2018 LEFT SINGLE QUOTATION MARK from strings like -
Ghulam ‘Ali, ‘Ali Khel,‘Ali Sher ‘Alaqahdari.
I want to remove occurrences of ‘A || ‘a || ‘U || ‘u in a string to A a U u respectively.
I tried
var myString = "Sozmah Qal‘ah"
var diacriticRemovedString = myString.folding(options: .diacriticInsensitive, locale: Locale.current)
print(diacriticRemovedString)
but it doesn't work.
Since the U+2018 character doesn't appear to be treated as a diacritic, you can simple search for such characters and remove them.
Here is the Swift 4 version (as specified in your original question) that removes diacritics and these specific quotation marks:
var myString = "Sozmah Qal‘ah"
var diacriticRemovedString = myString.folding(options: .diacriticInsensitive, locale: Locale.current).replacingOccurrences(of: "‘", with: "")
print(diacriticRemovedString)
Output:
Sozmah Qalah
Related
Let's say I have the following strings:
"Chest Stretch (left)"
"Chest Stretch (right)"
How can I use SwiftUI to output only:
"Chest Stretch"
I thought this may be a possible duplicate of swift - substring from string.
However, I am seeking a way to do this inside var body: some View within an if conditional expression.
A possible way is Regular Expression
let string = "Chest Stretch (left)"
let trimmedString = string.replacingOccurrences(of: "\\s\\([^)]+\\)", with: "", options: .regularExpression)
The found pattern will be replaced with an empty string.
The pattern is:
One whitespace character \\s
An opening parenthesis \\(
One or more characters which are not a closing parentheses [^)]+
and a closing parenthesis \\)
Or simpler if the delimiter character is always the opening parenthesis
let trimmedString = String(string.prefix(while: {$0 != "("}).dropLast())
Or
let trimmedString = string.components(separatedBy: " (").first!
I have a string like this in below and I want replace space with backslash and space.
let test: String = "Hello world".replacingOccurrences(of: " ", with: "\ ")
print(test)
But Xcode make error of :
Invalid escape sequence in literal
The code in up is working for any other character or words, but does not for backslash. Why?
Backslash is used to escape characters. So to print a backslash itself, you need to escape it. Use \\.
For Swift 5 or later you can avoid needing to escape backslashes using the enhanced string delimiters:
let backSlashSpace = #"\ "#
If you need String interpolation as well:
let value = 5
let backSlashSpaceWithValue = #"\\#(value) "#
print(backSlashSpaceWithValue) // \5
You can use as many pound signs as you wish. Just make sure to mach the same amount in you string interpolation:
let value = 5
let backSlashSpaceWithValue = ###"\\###(value) "###
print(backSlashSpaceWithValue) // \5
Note: If you would like more info about this already implemented Swift evolution proposal SE-0200 Enhancing String Literals Delimiters to Support Raw Text
I do not want numbers and punctuation marks to be entered in UITextField. I know how to do it but I need this;
What is regex expression that only blocks numbers and punctuation in Swift?
For example, the following only covers lowercase and uppercase letters as well as spaces.
let onlyCharacter = ". * [^ A-Za-z]. *"
let unAccesibleCharacters = NSCharacterSet.punctuationCharacters.union(NSCharacterSet.decimalDigits)
let yourText = "Test case"
if yourText.rangeOfCharacter(from: unAccesibleCharacters) == nil {
print("Your string contains only letters")
}
add " " to unAccesibleCharacters if spaces are not allowed too
How to remove Unicode U+2018 LEFT SINGLE QUOTATION MARK from strings like -
Ghulam ‘Ali, ‘Ali Khel,‘Ali Sher ‘Alaqahdari.
I want to remove occurrences of ‘A || ‘a || ‘U || ‘u in a string to A a U u respectively.
I tried
var myString = "Sozmah Qal‘ah"
var diacriticRemovedString = myString.folding(options: .diacriticInsensitive, locale: Locale.current)
print(diacriticRemovedString)
but it doesn't work.
Since the U+2018 character doesn't appear to be treated as a diacritic, you can simple search for such characters and remove them.
Here is the Swift 4 version (as specified in your original question) that removes diacritics and these specific quotation marks:
var myString = "Sozmah Qal‘ah"
var diacriticRemovedString = myString.folding(options: .diacriticInsensitive, locale: Locale.current).replacingOccurrences(of: "‘", with: "")
print(diacriticRemovedString)
Output:
Sozmah Qalah
Having a string like this:
let str = "In 1273, however, they lost their son in an accident;[2] the young Theobald was dropped by his nurse over the castle battlements.[3]"
I'm looking for a solution of removing all appearances of square brackets and anything that between it.
I was trying using a String's method: replacingOccurrences(of:with:), but it requires the exact substring it needs to be removed, so it doesn't work for me.
You can use:
let updated = str.replacingOccurrences(of: "\\[[^\\]]+\\]", with: "", options: .regularExpression)
The regular expression (without the required escapes needed in a Swift string is:
\[[^\]+]\]
The \[ and \] look for the characters [ and ]. They have a backslash to remove the normal special meaning of those characters in a regular expression.
The [^]] means to match any character except the ] character. The + means match 1 or more.
You can create a while loop to get the lowerBound of the range of the first string and the upperBound of the range of the second string and create a range from that. Next just remove the subrange of your string and set the new startIndex for the search.
var str = "In 1273, however, they lost their son in an accident;[2] the young Theobald was dropped by his nurse over the castle battlements.[3]"
var start = str.startIndex
while let from = str.range(of: "[", range: start..<str.endIndex)?.lowerBound,
let to = str.range(of: "]", range: from..<str.endIndex)?.upperBound,
from != to {
str.removeSubrange(from..<to)
start = from
}
print(str) // "In 1273, however, they lost their son in an accident; the young Theobald was dropped by his nurse over the castle battlements."