Strings

This notebook demonstrates some basic string commands.

Load Packages


In [1]:
using Dates

include("printmat.jl")


Out[1]:
printyellow (generic function with 1 method)

String Basics

The next few cells show how to

  1. combine several strings into one string by string(str1,str2) or str1 * str2.

  2. test if a string contains a specific substring

  3. replace part of a string with something else

  4. split a string into a vector of words (and then to join them back into a string again)

  5. sort a vector of words in alphabetical order


In [2]:
str1 = "Hello"
str2 = "world!\n"
str3 = "Where are you?"

str3b = string(str1," ",str2,str3)          #combine into one string
println(str3b)


Hello world!
Where are you?

In [3]:
str4 = "Highway 62 Revisited"

if occursin("Highway",str4)
    println("Yes, $str4 contains the word Highway")
end

str4 = replace(str4,"62" => "61")
println("\nNew, better string after a replacement: ",str4)


Yes, Highway 62 Revisited contains the word Highway

New, better string after a replacement: Highway 61 Revisited

In [4]:
words = split(str4)
println("split a string into a vector of words:")
printmat(words)

println("\nand join the words again into a string:")
println(join(words," "))


split a string into a vector of words:
   Highway
        61
 Revisited


and join the words again into a string:
Highway 61 Revisited

In [5]:
println("sort the words alphabetically")
sort(words,lt=isless)


sort the words alphabetically
Out[5]:
3-element Array{SubString{String},1}:
 "61"       
 "Highway"  
 "Revisited"

Reading an Entire File as a String

The next cell reads a file into one single string. It keeps the formatting (spaces, line breaks etc).


In [6]:
txtFile = "Data/FileWithText.txt"

fh1 = open(txtFile)              #open the file, can then refer to it as fh1
    str  = read(fh1,String)      #read as string
close(fh1)

In [7]:
println(typeof(str),"\n")

println(str)        #Printing the string read from a file


String

Dogs are nicer
than cats.
          
      This
      is a
fairly short file.

Reading all Lines of a File into an Array of Strings

The next cell reads a file into an array of strings: one string per line of the file. The second cell joins the lines into one string.


In [8]:
fh2 = open(txtFile)
    lines = readlines(fh2)
close(fh2)

printmat(lines)


Dogs are nicer
than cats.
          
      This
      is a
fairly short file.


In [9]:
linesJoined = join(lines,"\n")      #join the lines of the array,
println(linesJoined)                # "\n" to create line breaks


Dogs are nicer
than cats.
          
      This
      is a
fairly short file.

Strings and Indexing (extra)

can be tricky when the string contains non-ascii chacters.

Notice that you cannot change a string by indexing. For instance, str[1] = "D" does not work. However, you can read strings by indexing, if you are careful.

The next cell gives two versions of a string. Try running the subsequent cells for both versions.


In [10]:
str = "Dx = -0.9x"
#str = "Δx = -0.9x"             #uncomment this and re-run the cells below


Out[10]:
"Dx = -0.9x"

In [11]:
println(str[1])           #works
println(str[2])           #might not work, depending on the contents of string


D
x

If str[2] does not work, then that is due to the fact that the first character of the string takes more than one byte to store. Julia has commands to get around this. For instance, see the next cell.


In [12]:
#this should work in all cases
println(str[nextind(str,1)])    #nextind() gives the starting point of the next character


x

Looping over All Characters in a String


In [13]:
i = 1
for c in str               #alternatively, while i <= lastindex(str)
    #global i              #only needed in REPL/scripts 
    println(i," ",c)
    i = nextind(str,i)     #nextind() gives the starting point of the next character
end


1 D
2 x
3  
4 =
5  
6 -
7 0
8 .
9 9
10 x

In [ ]: