0

My problem is how to get character from a word

The result I needed is

DisplayChar("asd",1)

and it will display "a"

func DisplayChar(word : String, number : Int) -> String{
    let i: Int = count(word)
    var result = 0
    result = i - (i - number)
    var str = ""
    var j = 0

    for j = 0; j < result; j++ {
        str = str + word[j]
    }

    return str
}

DisplayChar("xyz", 2)
Aleksander Azizi
  • 9,829
  • 9
  • 59
  • 87
ZiSean
  • 127
  • 3
  • 9

3 Answers3

0

This code should work

let sentence = "Hello world"
let characters = Array(sentence)
print(characters[0]) // "H"
Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148
  • 1
    This [online playground](http://www.tutorialspoint.com/swift/try_swift.php) is still at 1.2. Only thing is it doesnt support modifications... So for every modification you have to copy, refresh the page, and paste before you can execute... but it works. :p – Eric Aya Jun 16 '16 at 19:01
  • Thanks, I also try find substring. – ZiSean Jun 16 '16 at 19:06
  • func DisplayChar(word : String, number : Int) -> String{ let result = word.substringToIndex(advance(word.startIndex,number)) return result } DisplayChar("xyz", 2) – ZiSean Jun 16 '16 at 19:07
  • 1
    @EricD: thank you. I updated my answer thanks to your link. – Luca Angeletti Jun 16 '16 at 19:11
  • Thank you, but return String(word[number - 1]) this line of code in swift 1.2 it pop out subscript is unavailable error – ZiSean Jun 16 '16 at 19:14
0

There are a couple good solutions in this answer that may work, two good ones duplicated below.

Convert to Array

let word = "test"    
var firstChar = Array(word)[0] // t

(Note: this assumes a UTF8 or ASCII encoded string, but that is likely fine for school.)

Create Your Own Extension

First an extension of String to handle subscripts:

extension String {

  subscript (i: Int) -> Character {
    return self[self.startIndex.advancedBy(i)]
  }

  subscript (i: Int) -> String {
    return String(self[i] as Character)
  }

  subscript (r: Range<Int>) -> String {
    let start = startIndex.advancedBy(r.startIndex)
    let end = start.advancedBy(r.endIndex - r.startIndex)
    return self[Range(start ..< end)]
  }
}

Then you can just use:

let word = "test"    
var firstChar = word[0] // t
Community
  • 1
  • 1
Carter
  • 3,053
  • 1
  • 17
  • 22
  • func DisplayChar(word : String, number : Int) -> Character{ let i: Int = count(word) var result = 0 result = i - (i - number) let str = Array(word) var j = 0 for j = 0; j < result; j++ { print(str[j]) } return str } DisplayChar("xyz", 2) I try to convert it to Array character but return type is string, how can I convert to string back again – ZiSean Jun 16 '16 at 19:23
  • Right now it looks like you are printing every character up to `number` and returning the full array of characters. From your initial post, it looks like you want to print just one character, in that case your print statement should not be in a loop. Can you describe what you expect `DisplayChar` to print and return? – Carter Jun 16 '16 at 19:29
  • the result of the code is like this when user key in Display("xyz", 2) the result become xy when user key in Display("xyz", 1) the result become x – ZiSean Jun 16 '16 at 19:32
  • Okay, don't print the string in the loop. Have some `var returnString = ""` and in the loop do `returnString = returnString + str[j]`. Then return the `returnString` at the end of the function. – Carter Jun 16 '16 at 19:35
  • Thanks, I will try tmr, coz is midnight 4am, i need to attend my class at 8.30pm..... – ZiSean Jun 16 '16 at 19:42
-1

Swift strings have a method called substringToIndex, "asd".substringToIndex(1) will return "a".

I'm not sure if it works on Swift 1.2, though.

EmilioPelaez
  • 18,758
  • 6
  • 46
  • 50