Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 642 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Swift 3.0 iterate over String.Index range

#1
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])
}
Reply

#2
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.
Reply

#3
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"
Reply

#4
**Swift 4.2**

Simply:

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

Reply

#5
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`
Reply

#6
Swift 4:

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

#7
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
Reply

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

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

Reply

#9
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)
}
Reply

#10
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]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through