0Day Forums
Swift 3.0 iterate over String.Index range - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: Swift (https://0day.red/Forum-Swift)
+--- Thread: Swift 3.0 iterate over String.Index range (/Thread-Swift-3-0-iterate-over-String-Index-range)

Pages: 1 2


Swift 3.0 iterate over String.Index range - disbudcyqgb - 07-19-2023

The following was possible with Swift 2.2:

let m = "alpha"
for i in m.startIndex..<m.endIndex {
print(m[i])
}
a
l
p
h
a


With 3.0, we get the following error:

> Type 'Range<Index>' (aka 'Range<String.CharacterView.Index>') does not conform to protocol 'Sequence'

I am trying to do a very simple operation with strings in swift -- simply traverse through the first half of the string (or a more generic problem: traverse through a range of a string).

I can do the following:

let s = "string"
var midIndex = s.index(s.startIndex, offsetBy: s.characters.count/2)
let r = Range(s.startIndex..<midIndex)
print(s[r])

But here I'm not really traversing the string. So the question is: how do I traverse through a range of a given string. Like:

for i in Range(s.startIndex..<s.midIndex) {
print(s[i])
}


RE: Swift 3.0 iterate over String.Index range - prototrophs458149 - 07-19-2023

When iterating over the indices of characters in a string, you seldom only need the index. You probably also need the character at the given index. As specified by Paulo (updated for Swift 4+), `string.indices` will give you the indices of the characters. `zip` can be used to combine index and character:

let string = "string"

// Define the range to conform to your needs
let range = string.startIndex..<string.index(string.startIndex, offsetBy: string.count / 2)
let substring = string[range]
// If the range is in the type "first x characters", like until the middle, you can use:
// let substring = string.prefix(string.count / 2)

for (index, char) in zip(substring.indices, substring) {
// index is the index in the substring
print(char)
}

Note that using `enumerated()` will produce a pair of index and character, but the index is *not* the index of the character in the string. It is the index in the enumeration, which can be different.



RE: Swift 3.0 iterate over String.Index range - provostess407443 - 07-19-2023

Use the following:

for i in s.characters.indices[s.startIndex..<s.endIndex] {
print(s[i])
}

Taken from [Migrating to Swift 2.3 or Swift 3 from Swift 2.2][1]


[1]:

[To see links please register here]

"Migrating to Swift 2.3 or Swift 3 from Swift 2.2"


RE: Swift 3.0 iterate over String.Index range - fredric208 - 07-19-2023

**Swift 4.2**

Simply:

let m = "alpha"
for i in m.indices {
print(m[i])
}




RE: Swift 3.0 iterate over String.Index range - hilton109615 - 07-19-2023

To concretely demonstrate how to traverse through a range in a string in Swift 4, we can use the `where` filter in a `for` loop to filter its execution to the specified range:

func iterateStringByRange(_ sentence: String, from: Int, to: Int) {

let startIndex = sentence.index(sentence.startIndex, offsetBy: from)
let endIndex = sentence.index(sentence.startIndex, offsetBy: to)

for position in sentence.indices where (position >= startIndex && position < endIndex) {
let char = sentence[position]
print(char)
}

}

`iterateStringByRange("string", from: 1, to: 3)` will print `t`, `r` and `i`


RE: Swift 3.0 iterate over String.Index range - glynisym - 07-19-2023

Swift 4:

let mi: String = "hello how are you?"
for i in mi {
print(i)
}


RE: Swift 3.0 iterate over String.Index range - swampiness767851 - 07-19-2023

You can traverse a string by using `indices` property of the `characters` property like this:

let letters = "string"
let middle = letters.index(letters.startIndex, offsetBy: letters.characters.count / 2)

for index in letters.characters.indices {

// to traverse to half the length of string
if index == middle { break } // s, t, r

print(letters[index]) // s, t, r, i, n, g
}

From the [documentation](

[To see links please register here]

) in section *Strings and Characters - Counting Characters*:
> Extended grapheme clusters can be composed of one or more Unicode scalars. This means that different characters—and different representations of the same character—can require different amounts of memory to store. Because of this, characters in Swift do not each take up the same amount of memory within a string’s representation. **As a result, the number of characters in a string cannot be calculated without iterating through the string** to determine its extended grapheme cluster boundaries.

emphasis is my own.

This will not work:

let secondChar = letters[1]
// error: subscript is unavailable, cannot subscript String with an Int



RE: Swift 3.0 iterate over String.Index range - diastrophisms599398 - 07-19-2023

Iterating over characters in a string is cleaner in Swift 4:

let myString = "Hello World"
for char in myString {
print(char)
}




RE: Swift 3.0 iterate over String.Index range - jilleenks - 07-19-2023

Another option is to use `enumerated()` e.g:

let string = "Hello World"
for (index, char) in string.characters.enumerated() {
print(char)
}
or for Swift 4 just use

let string = "Hello World"
for (index, char) in string.enumerated() {
print(char)
}


RE: Swift 3.0 iterate over String.Index range - jaynieejeidjhv - 07-19-2023

The best way to do this is :-

let name = "nick" // The String which we want to print.

for i in 0..<name.count
{
// Operation name[i] is not allowed in Swift, an alternative is
let index = name.index[name.startIndex, offsetBy: i]
print(name[index])
}

for more details visit [here][1]


[1]:

[To see links please register here]