NSRange in Strings having dialects

无人久伴 提交于 2020-06-02 06:09:27

问题


I was working on an app, which takes input in a language called "Tamil". So in order to find the range of any particular charater in the string i have used the below code.

var range = originalWord.rangeOfString("\(character)")
println("\(range.location)")

So this works fine except for some cases.

there are some characters like this -> í , ó . // am just saying an example.

So like this combination, in other languages there are several vowel diacritcs are there.

If i have this word "alv`in"

// which is alvin , but i used "v" with a dialect. If i print the unicde value of these characters in xcode, i will get each unicode. But for "v`" there will be two unicode values but its considered as a single character.

So if i check this character in the above mentioned code. i get the folowing result. Which gives errors in my program.

range.location // 2147483647 , its not a single digit.? why.?

But for other characters its just prints the correct Int Value. // Single digit like "3"

Anybody have any idea of how to get this done.? How can i achieve this if i use characters with dialets

.?

code given below

// userInput = "இல்லம்"

var originalWord : NSString = ("இல்லம்")

   var originalArray = Array("இல்லம்")

        var userInputWord = Array(String(userInput))



       // -------------------------------------------

        for character in String(userInput)
        {
            switch character
            {

            case originalArray[0] :

                // here matches first character of the userinput to the original word first character

                // the character exists at the 0th index


                var range = originalWord.rangeOfString("\(character)")


                if range.location == 0
                {
                     // same character in the same index
                    // correctValue increase by one (cow Value)

                    cowValue += 1

                }
                else
                {
                    // same character but in the different index 
                    // Wrong value increase by one (bull Value)

                    bullValue += 1
                }


            case originalArray[1] :

                // here matches first character of the userinput to the original word first character

                // the character exists at the 1th index


                var range = originalWord.rangeOfString("\(character)")

                println("\(range.location)") // here i get he long Int Value instead of single digit


                if range.location == 1
                {
                    // same character in the same index
                    // correctValue increase by one (cow Value)

                    cowValue += 1

                }
                else
                {
                    // same character but in the different index
                    // Wrong value increase by one (bull Value)

                    bullValue += 1
                }

回答1:


You should use Swift strings instead of NSString, because Swift strings have full Unicode support including composed character sequences, (extended) grapheme clusters etc.

For Swift strings, rangeOfString() returns an optional Range<String.Index> which is a bit more complicated to handle. You can also use find() instead to find the position of a character. This might help as a starting point:

var cowValue = 0
var bullValue = 0

let userInput = "இல்லம்"
let originalWord = "இல்லம்"
let originalArray = Array("இல்லம்")

for character in userInput {
    switch character {
    case originalArray[0] :
        if let pos = find(originalWord, character) {
            // Character found in string
            println(pos)
            if pos == originalWord.startIndex {
                // At position 0
                cowValue += 1
            } else {
                // At a different position
                bullValue += 1
            }
        } else {
            // Character not found in string
        }

    case originalArray[1] :
        if let pos = find(originalWord, character) {
            // Character found in string
            println(pos)
            if pos == advance(originalWord.startIndex, 1) {
                // At position 1
                cowValue += 1
            } else {
                // At a different position
                bullValue += 1
            }
        } else {
            // Character not found in string
        }
    default:
        println("What ?")
    }
}



回答2:


Check out the documentation for NSString's rangeOfComposedCharacterSequenceAtIndex: and rangeOfComposedCharacterSequencesForRange:

You want to look for Composed Character Sequences, not individual characters.



来源:https://stackoverflow.com/questions/26281519/nsrange-in-strings-having-dialects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!