Khwaja Moiz
Khwaja Moiz

Reputation: 89

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

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

Upvotes: 0

Views: 3307

Answers (5)

Vipul Verma
Vipul Verma

Reputation: 123

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))

Upvotes: 1

M.zar
M.zar

Reputation: 9

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]
}

Upvotes: 0

Joseph Lord
Joseph Lord

Reputation: 6504

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.

Upvotes: 3

Michael Dorner
Michael Dorner

Reputation: 20175

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

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

Upvotes: 2

Arc676
Arc676

Reputation: 4465

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

Upvotes: 0

Related Questions