1

I wanted to perform a if condition for each character in a string, the string consists of digits and alphabets so I want to separate digits by using if condition, so how to extract each character and add it to another string I've tried to convert using

NSString 

But even then It didn't work so is there anything like

toInt() 
1
  • 2
    toInt() for a character? What do you have, what do want to achieve? Commented Apr 14, 2015 at 8:19

1 Answer 1

3

You can cast directly to swift String:

let c : Character = "c" let str = String(c) 

You can not access a character at a index of string with str[index], you have to access by a Range. If you want to access this way, add a subscript extension to String:

extension String { subscript (index: Int) -> Character { return self[advance(self.startIndex, index)] } } 

then, you can call:

let myString = "abcdef" let c: Character = myString[1] //return 'b' 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.