DVNold
DVNold

Reputation: 929

How to iterate over the lines in a string?

I have a long string in Julia. I'd like to apply some operation to each line. How can I efficiently iterate over each line? I think I can use split but I am wondering if there is a method that won't allocate all the strings upfront?

Upvotes: 8

Views: 1385

Answers (1)

pfitzseb
pfitzseb

Reputation: 2554

You can use eachline for this:

julia> str = """
       a
       b
       c
       """
"a\nb\nc\n"

julia> for line in eachline(IOBuffer(str))
         println(line)
       end
a
b
c

There's also a version that operates directly on a file, in case that's relevant to you:

help?> eachline
search: eachline eachslice

  eachline(io::IO=stdin; keep::Bool=false)
  eachline(filename::AbstractString; keep::Bool=false)

  Create an iterable EachLine object that will yield each line from an I/O stream or a file. Iteration calls readline on
  the stream argument repeatedly with keep passed through, determining whether trailing end-of-line characters are
  retained. When called with a file name, the file is opened once at the beginning of iteration and closed at the end. If
  iteration is interrupted, the file will be closed when the EachLine object is garbage collected.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> open("my_file.txt", "w") do io
             write(io, "JuliaLang is a GitHub organization.\n It has many members.\n");
         end;
  
  julia> for line in eachline("my_file.txt")
             print(line)
         end
  JuliaLang is a GitHub organization. It has many members.
  
  julia> rm("my_file.txt");

If you already have the complete string in memory then you can (and should) use split, as pointed out in the comments. split basically indexes into the string and doesn't allocate new Strings for each line, as opposed to eachline.

Upvotes: 12

Related Questions