0

I just started with Swift a few days ago and I am struggling with all the different Datatypes...

Lets say we do this:

var myarray = ["justin", "steve", "peter"]
var newString = myarray[2]

so why I cant now print just the "p" from "peter"?

print(newString[0])

---> gives me an error:

"'subscript' is unavailable: cannot subscript String with an Int"

in this topic: [Get nth character of a string in Swift programming language

it says: "Note that you can't ever use an index (or range) created from one string to another string"

But I cant imagine, that there isn't way to handle it...

Because When I do this:

var myarray = ["justin", "steve", "p.e.t.e.r"]
var newString = myarray[2]
let a : [String] = newString.componentsSeparatedByString(".")
print(a[2])
  • then it works. It prints (in this case) "t".

So how I can split it "SeparatedByString" 'Nothing'?

Im sure to solve this will help me also at many other problems. I hope I postet the question in the way it should be done.

Thank you for any solution or tips :)

0

1 Answer 1

2

The instance method componentsSeparatedByString applied to a String instance yields an array of String instances, namely [String]. The elements of an array can be indexed in the classic Java/C++ fashion (let thirdElement = array[2]).

If you'd like to split your String instance ("peter") into an array of single character String instances, you can make use of the CharacterView property of a String instance, and thereafter map each single Character in the CharacterView back to a single-character String instance

let str = "peter"
let strArray = str.characters.map(String.init(_:)) // ["p", "e", "t", "e", "r"]
let thirdElement = strArray[2] // t

This is quite roundabout way though (String -> CharacterView -> [String] -> String), and you're most likely better off simply looking at the excellent answer (covering direct String indexing) in the dupe thread dug up by @Hamish:

The possible corner case where the above could be applicable, as pointed out by Hamish below, is if you very frequently need access to characters in the String at specific indices, and like to make use of the O(1) random access available to arrays (more so applicable if you are working with immutable String:s, such that the corresponding String array only needs to be generated once for each immutable String instance).

Sign up to request clarification or add additional context in comments.

16 Comments

This is a good approach if you need to frequently access the characters at a given index though, without having to do a linear walk for each access :)
Thank you. :) it gives me 2 Errors ("Expected ',' separator" "Expected expression in list of expressions") but it's maybe a Problem of my Swift version (2.1.1). I'll also check the linked thread. Thanks, helped me to not rage! :D
@Hamish that's true! I don't really know what I really think of the .map(String.init(_:)) variation though, as compared to .map { String($0) }, but hey, why not :D
@scopaest replace the 2nd line in the snippet above with let strArray = str.characters.map { String($0) } and it might work for Swift 2.1 (tested and working for Swift 2.2).
@dfri Yeah, I usually go back to using a closure expression when it gets to the point where you have to disambiguate the init with (_:), but I guess it's a matter of preference :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.