In [1]:
using Dates
include("printmat.jl")
Out[1]:
The next few cells show how to
combine several strings into one string by string(str1,str2)
or str1 * str2
.
test if a string contains a specific substring
replace part of a string with something else
split a string into a vector of words (and then to join them back into a string again)
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)
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)
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," "))
In [5]:
println("sort the words alphabetically")
sort(words,lt=isless)
Out[5]:
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
In [8]:
fh2 = open(txtFile)
lines = readlines(fh2)
close(fh2)
printmat(lines)
In [9]:
linesJoined = join(lines,"\n") #join the lines of the array,
println(linesJoined) # "\n" to create line breaks
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]:
In [11]:
println(str[1]) #works
println(str[2]) #might not work, depending on the contents of string
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
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
In [ ]: