Find indices of white space characters in a string This function returns the indices of all white spaces as an array of Integer. It works fine with a small string:

func whiteSpacesIndices(value : String) -&gt; Array&lt;Int&gt; {
    var indices: Array&lt;Int&gt; = []
    for (index, char) in value.enumerated() {
        if char == " " || char == "\n" || char == "\t" {
            indices.append(index)
        }
    }
    return indices
}


However when the string is too long, it could be very slow, because it is looping in every character.

Is there a better way for doing it?
General remarks


value, as a parameter name, isn't very descriptive,
The code considers that white spaces can only be " " or "\n" or "\t". This a performance optimization and supposes prior knowledge of the contents of the string. More generally you could make the check this way:

if char.isWhitespace {
    indices.append(index)
}

Array&lt;Int&gt; is not the same as [String.Index] of IndexSet. A String can be traversed using String.Index and not and Int. 


Performance

The following codeis twice as fast in my tests, but doesn’t work with emoji :

func whiteSpacesIndices(in str : String) -&gt; Array&lt;Int&gt; {
    var indices: Array&lt;Int&gt; = []
    let blanks: [UInt32] = [32, 10, 9]  //these values correspond to space, new line, and tabulation respectively.
    for (index, scalar) in str.unicodeScalars.enumerated() {
        if blanks.contains(scalar.value) {
            indices.append(index)
        }
    }
    return indices
}


You can learn more about the Unicode scalar representation here.

Free function or instance method?

The whiteSpacesIndices function seems more like a property on strings. It is appropriate for a String to know about the indices of white spaces (and new lines) within itself:

extension String {
    var whiteSpaceIndices: [Int] {
        var indices = [Int]()
        let blanks: [UInt32] = [32, 10, 9]
        for (index, scalar) in self.unicodeScalars.enumerated() {
            if blanks.contains(scalar.value) {
                indices.append(index)
            }
        }
        return indices
    }
}


And could be used like so:

"Hello world!".whiteSpaceIndices    //[5]
"ä ö ü".whiteSpaceIndices           //[1, 3]