liveBook · Manning

Split

As explained in Julia as a Second Language

split is a Julia function that divides a string into substrings based on a delimiter, character predicate, or whitespace.

`split` turns a Julia string into an array of parts. It is handy when a line of input contains fields separated by a character: `split("hawaiian,S,10.5", ',')` produces `"hawaiian"`, `"S"`, and `"10.5"`. section 5.3.2

The parts are still text. To calculate with the price, convert it afterward: `parse(Float64, "10.5")` gives `10.5`. In a file-reading loop, `readline` gets each record, `split` separates its fields, and `parse` converts fields that need numeric values. section 5.3.2 section 17.1

Splitting on a condition

A character-testing function can choose split points: `split("The quick brown fox", isspace)` splits on whitespace. But the matching characters disappear. For `"oneTwoThreeFour"`, `split(s, isuppercase)` yields `"one"`, `"wo"`, `"hree"`, and `"our"`—not the words needed for a camel-case conversion. Use `findall(isuppercase, s)` and string ranges when those capital letters must be kept. section 11.2.1

Listing 5.8 Loading pizza sales data
function load_pizzatable(io)
    pizzas = []                               #1
    readline(io)                              #2
    while !eof(io)                            #3
      pz = split(readline(io), ',')
      pr = parse(Float64, price(pz))          #4
      sz = portion(pz)
      push!(pizzas, (name(pz), sz[1], pr))    #5
    end
    pizzas
end

A complete pizza-sales loader shows `split` between reading a line and parsing its price. section 5.3.2

How Julia as a Second Language covers it

How deep it goes
Demonstrates character and predicate delimiters, substring results, and discarded split characters through runnable examples.
Examples
Uses pizza-sales and rocket-engine records, whitespace splitting, and a failed camel-case split.
What it assumes
Assumes basic Julia strings and function calls; introduces line reading and numeric parsing alongside the file-processing example.
What you can do after
Build it from scratch: separate simple comma-delimited lines into fields and parse numeric fields for a usable record.
How it connects
Connects `readline` and `parse` in CSV-style input processing, and contrasts with `findall` and string ranges for camel-case conversion.

Also explained in

Compare how 11 books explain split →

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.