How to remove special characters from string in Swift 2?
Like this:
func removeSpecialCharsFromString(text: String) -> String {
let okayChars : Set<Character> =
Set("abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLKMNOPQRSTUVWXYZ1234567890+-*=(),.:!_".characters)
return String(text.characters.filter {okayChars.contains($0) })
}
And here's how to test:
let s = removeSpecialCharsFromString("père") // "pre"
Swift – How to remove special character without remove emoji from string?
You can use below extension to make your sentence perfect as per desired output.
extension String {
var condensedWhitespace: String {
let components = self.components(separatedBy: NSCharacterSet.whitespacesAndNewlines)
return components.filter { !$0.isEmpty }.joined(separator: " ")
}
func removeSpecialCharacters() -> String {
let okayChars = CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLKMNOPQRSTUVWXYZ1234567890 ")
return String(self.unicodeScalars.filter { okayChars.contains($0) || $0.properties.isEmoji })
}
}
Example.
let input = "Hello guys !? Need some money $ br>
print(input.removeSpecialCharacters().condensedWhitespace)
// Hello guys Need some money br>
Remove special characters from the string
placeAnnotation.mapItem.phoneNumber!.components(separatedBy: CharacterSet.decimalDigits.inverted)
.joined()
Here you go!
I tested and works well.
How to remove special characters in Swift 3
Working in Swift 4.1
import Foundation
extension String {
var withoutSpecialCharacters: String {
return self.components(separatedBy: CharacterSet.symbols).joined(separator: "")
}
}
How to remove special character \u{e2} from string
Thank you every one for your help. You are so kind
I am able to find the invalid character using
phoneNumber?.map{$0.unicodeScalars.allSatisfy{$0.isASCII}}
and it returns
Optional<Array<Bool>>
▿ some : 12 elements
- 0 : false
- 1 : true
- 2 : true
- 3 : true
- 4 : true
- 5 : true
- 6 : true
- 7 : true
- 8 : true
- 9 : true
- 10 : false
- 11 : true
I am able to fix this issue using one line
phoneNumber?.filter{$0.unicodeScalars.allSatisfy{$0.isASCII}}
and Count is 10
using
phoneNumber?.filter{$0.unicodeScalars.allSatisfy{$0.isASCII}}.count
Hope it is helpful to others :)
How to strip special characters out of string?
Let's write a function for that (in swift 1.2).
func stripOutUnwantedCharactersFromText(text: String, set characterSet: Set<Character>) -> String {
return String(filter(text) { set.contains($0) })
}
You can call it like that:
let text = "American Samoa"
let chars = Set("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
let strippedText = stripOutUnwantedCharactersFromText(text, set: chars)
Pure swift. Yeah.
How to remove special characters from the `TextField` in SwiftUI
How about a custom TextField with checks, instead of messing with Binding Wrapper itself:
(with OPs full code to proof that binding works)
struct ContentView: View {
@State var name = ""
@State var number = ""
var body: some View {
InputField(text: $name, number: $number)
}
}
struct InputField: View {
@Binding var text: String
@Binding var number: String
var body: some View {
VStack {
TextFieldWithCheck("Name here...", text: $text, limit: 15, allowed: .alphanumerics)
.frame(maxWidth: .infinity, minHeight: 48, alignment: .leading)
TextFieldWithCheck("Phone no here...", text: $number, limit: 9, allowed: .decimalDigits)
.frame(maxWidth: .infinity, minHeight: 48, alignment: .leading)
}.padding()
}
}
// here is the custom TextField itself
struct TextFieldWithCheck: View {
let label: LocalizedStringKey
@Binding var text: String
let limit: Int
let allowed: CharacterSet
init(_ label: LocalizedStringKey, text: Binding<String>, limit: Int = Int.max, allowed: CharacterSet = .alphanumerics) {
self.label = label
self._text = Binding(projectedValue: text)
self.limit = limit
self.allowed = allowed
}
var body: some View {
TextField(label, text: $text)
.onChange(of: text) { _ in
// all credits to Leo Dabus:
text = String(text.prefix(limit).unicodeScalars.filter(allowed.contains))
}
}
}
Related Topics
Why Is This Code Not Recognising the Nsstring as Being Equal
Datefromstring Returns Nil for Some Values
Async Request Does Not Enter Completion Block
"Too Many Symbol Files" After Successfully Submitting My Apps
Run iPhone as an Ibeacon in the Background
How to Invoke iPhone Maps for Directions with Current Location as Start Address
Is There Any Way of Asking an iOS View Which of Its Children Has First Responder Status
How to Hide 'Back' Button on Navigation Bar on Iphone
How to Present View Controller from Right to Left in iOS Using Swift
How to Directly Rotate Cvimagebuffer Image in iOS 4 Without Converting to Uiimage
Changing View Controller When Segmented Control Changes
Convert Nsdate to String in iOS Swift
Ios: How to Run a Function After Device Has Rotated (Swift)
Loading Viewcontroller from Xib File