Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

How does String substring work in Swift

I've been updating some of my old code and answers with Swift 3 but when I got to Swift Strings and Indexing with substrings things got confusing.

Specifically I was trying the following:

let str = "Hello, playground"
let prefixRange = str.startIndex..<str.startIndex.advancedBy(5)
let prefix = str.substringWithRange(prefixRange)

where the second line was giving me the following error

Value of type 'String' has no member 'substringWithRange'

I see that String does have the following methods now:

str.substring(to: String.Index)
str.substring(from: String.Index)
str.substring(with: Range<String.Index>)

These were really confusing me at first so I started playing around index and range. This is a followup question and answer for substring. I am adding an answer below to show how they are used.

Answer*

Draft saved
Draft discarded
Cancel
25
  • 6
    The indexes are very useful because a character can be more than one byte. Try let str = "🇨🇭🇩🇪🇺🇸Hello" print(str.substring(to: 2)) Commented Sep 24, 2016 at 15:15
  • 175
    Yes, I understand that a character (i.e. extended grapheme cluster) can take multiple bytes. My frustration is why we have to use the verbose index-advancing method to access the characters of a string. Why can't the Swift team just add some overloads to the Core Library to abstract it away. If I type str[5], I want to access the character at index 5, whatever that character appears to be or how many bytes it takes. Isn't Swift all about developer's productivity? Commented Sep 24, 2016 at 15:51
  • 8
    @RenniePet I believe Apple recognizes the problem and changes are coming. As per the Swift Evolution page on GitHub: "Swift 4 seeks to make strings more powerful and easier-to-use, while retaining Unicode correctness by default". It's vague but let's keep our hopes up Commented Feb 23, 2017 at 2:17
  • 3
    @CodeDifferent why apple didn't add subscript character access? So that people understand that it's bad thing to do. Basically if you would do for i in 0..string.count using subscripts that would be double loop, cause under the hood index has to go through each byte of string to find out which is the next character. If you loop using index, you iterate over string only once. Btw, hate this myself, but that's the reasoning behind subscript being not available on string in swift. Commented Nov 26, 2017 at 15:54
  • 8
    @RaimundasSakalauskas that argument doesn't fly by me. C# has both Unicode correctness and integer subscripting, which is really convenient. In Swift 1, Apple wanted developers to use countElement(str) to find the length. In Swift 3, Apple made string not conforming to Sequence and forced everyone to use str.characters instead. These guys are not afraid of making changes. Their stubbornness on integer subscripting in really hard to understand Commented Nov 26, 2017 at 16:01

lang-swift

AltStyle によって変換されたページ (->オリジナル) /