Cannot find char at position and index of char of a string var in Swift 2

夙愿已清 提交于 2021-01-07 03:12:43

问题


Equivalent functions in Swift 2 of Java's charAt() and indexOf() ?


回答1:


Did you read the NSString documentation? String and NSString are not identical, but as mentioned by this answer most functions are more or less the same. Anyway, the two exact functions you ask for are right there:

Java's charAt:

func characterAtIndex(_ index: Int) -> unichar

Java's indexOf:

func rangeOfString(_ searchString: String) -> NSRange

The NSRange data type contains just 2 variables: location and length that tell you where the substring is in the original string and how long it is. See the documentation.

//Java
"abc".indexOf("b") => 1
//Swift
"abc".rangeOfString("b").location #=> 1



回答2:


Firstly read this article about Swift strings and think about exactly what you mean by characters.

You can use the character view (or the utf16 view if that is the sort of characters that you want) of the string to see it as a collection and if you really need to get characters by index (rather than by iteration) you may want to convert it to an array but normally you just need to advance the index.

let myString = "Hello, Stack overflow"

// Note this index is not an integer but an index into a character view (String.CharacterView.Index)
let index = myString.characters.indexOf( "," )

let character = myString[myString.startIndex.advancedBy(4)] // "o"

This is O(n) (where n is the number of characters into the String) as it needs to iterate over the array as Characters may vary in length in the encoding)

Old answer below. The character array may be quicker for repeat access still as the array accesses are O(1) following the one off O(n) conversion to array (n is the array length).

let cIndex = 5
// This initialises a new array from the characters collection
let characters = [Character](myString.characters)
if cIndex < characters.count {
    let character = characters[cIndex]
    // Use the character here
}

Obviously some simplification is possible if the index is guaranteed to be within the length of the characters but I prefer to demonstrate with some safety on SO.




回答3:


You can extend the String class with the missing charAt(index: Int) function:

extension String {
    func charAt(index: Int) -> Character {
        return [Character](characters)[index]
    }
}



回答4:


You have to convert the input string to an Array then return the character from the specific index here is the code

extension String {
   func charAt(_ index : Int) -> Character {
      let arr = Array(self.characters);
      return (arr[index]);
   }
}

This will work same as Java's charAt() method you can use it as

var str:String = "Alex"
print(str.charAt(1))



回答5:


you can used it instead of CharAt() in swift 3:

func charAt(str: String , int :Int)->Character{

    str[str.startIndex]
    let index = str.index(str.startIndex, offsetBy: int)

    return str[index]
}


来源:https://stackoverflow.com/questions/33391777/cannot-find-char-at-position-and-index-of-char-of-a-string-var-in-swift-2

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