1

I need to find accents character from string , Not remove character.

I tried with following code but don't know how to identified that is accents character?

var str = "Hello, plaAyground"
    var scalars = str.unicodeScalars

    str = "Hello, plåÅyground"
    scalars = str.unicodeScalars
    print(scalars.dropFirst())
GIJOW
  • 2,307
  • 1
  • 17
  • 37
3JT
  • 13
  • 5
  • 1
    Possible duplicate of [Check if string contains special characters in Swift](https://stackoverflow.com/questions/27703039/check-if-string-contains-special-characters-in-swift) – GIJOW Apr 13 '18 at 12:53
  • 1
    You can check this link. it might be helpful for you.https://stackoverflow.com/questions/29521951/how-to-remove-diacritics-from-a-string-in-swift?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Hiren Rathod Apr 13 '18 at 12:53
  • special characters searching is not required, and remove accent also... Actually I want to find accent character and also need to know that accent character is in Upper accent case or Lower accent case – 3JT Apr 13 '18 at 13:09
  • Do you use just the ASCII set of characters or you use also unicode chars ? – Sergiob Apr 13 '18 at 14:45

1 Answers1

1

You can create a custom CharacterSet that contains all the characters you need, and then search your target string for occurrences of characters from that set

let str = "Hello, plåÅyground"
let characters = "åÅ" // put all required characters in that string
let characterSet = CharacterSet(charactersIn: characters)
if let _ = str.rangeOfCharacter(from: characterSet) {
     // do stuff here
}

If you want to differentiate between occurrences of uppercase/lowercase, create separate character sets for uppercase/lowercase characters

let str = "Hello, plåÅyground"
let uppercaseCharacters = "Å"
let lowercaseCharacters = "å"
let uppercaseCharacterSet = CharacterSet(charactersIn: uppercaseCharacters)
let lowercaseCharacterSet = CharacterSet(charactersIn: lowercaseCharacters)
if let _ = str.rangeOfCharacter(from: uppercaseCharacterSet) {
     // uppercase character found
} else if let _ = str.rangeOfCharacter(from: lowercaseCharacterSet) {
    // lowercase character found
}
mag_zbc
  • 6,801
  • 14
  • 40
  • 62
  • I am solved using your concept but I am not able to post solution over here due to less reputation point – 3JT Apr 16 '18 at 06:53