liveBook · Manning

Nextind

As explained in Julia as a Second Language

nextind is a Julia function that returns the next valid index for an indexed collection or string.

nextind returns the next valid index for an indexed collection or string. It matters because a character’s position in a Unicode string may skip code-unit positions. section 11.1.1

Walk a string safely

s = "Þrúðvangar"; i = firstindex(s); while i <= lastindex(s); println((i, s[i])); i = nextind(s, i); end. The indices progress 1, 3, 4, 6, 8, and so on, because some characters use multiple UTF-8 code units. section 11.1.1

For arrays, nextind is simply the current integer index plus one. For ordinary character iteration, for loops automatically advance by whole characters; use eachindex when the indices themselves are needed. section 11.1.1

Listing 11.1 Iterating over characters in a string using a while loop
julia> s = "Þrúðvangar"
 
julia> i = firstindex(s)
1
 
julia> while i <= lastindex(s)
           println((i, s[i]))
           i = nextind(s, i)
       end
(1, 'Þ')
(3, 'r')      #1
(4, 'ú')
(6, 'ð')      #2
(8, 'v')
(9, 'a')
(10, 'n')
(11, 'g')
(12, 'a')
(13, 'r')

The runnable loop makes Unicode-aware index advancement visible. section 11.1.1

How Julia as a Second Language covers it

How deep it goes
Defines index advancement, demonstrates array and string loops, and shows implementation differences without requiring routine manual use.
Examples
Uses [4, 5, 3] and "Þrúðvangar" to print index-value pairs, exposing skipped UTF-8 positions.
What it assumes
Assumes basic Julia indexing and loops, while illustrating Unicode indexing through concrete examples.
What you can do after
Apply it to advance safely through indexed collections, while preferring eachindex or for loops for ordinary character iteration.
How it connects
Connects strings, arrays, Unicode, UTF-8 code units, firstindex, lastindex, eachindex, and for loops.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.