Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia: comparing strings with special characters

Tags:

string

julia

I need to read a text file which contains csv data with headers separating individual blocks of data. The headers always start with the dollar sign $. So my text file looks like:

$Header1
2
1,2,3,4
2,4,5,8
$Header2
2
1,1,0,19,9,8
2,1,0,18,8,7

What I want to do is if the program reaches to $Header2, I want to read all the next lines following it till it reaches, say, $Header3 or end of the file. I think I can use `cmp' in Julia for this. I tried with a small file that contains following text:

# file julia.txt
Julia
$Julia

and my code reads:

# test.jl
fname = "julia.txt"
# set some string values
str1 ="Julia";
str2 ="\$Julia";
# print the strings and check the length
println(length(str1),",",str1);
println(length(str2),",",str2);
# now read the text file to check if you are able to find the strings
# str1 and str2 above
println ("Reading file...");
for ln in eachline(fname)
 println(length(ln),",",ln);
 if (cmp(str1,ln)==0)
  println("Julia match")
 end
 if (cmp(str2,ln)==0)
  println("\$Julia match")
 end
end

what I get as output from the above code is:

5,Julia
6,$Julia
Reading file...
6,Julia

7,$Julia

I don't understand why I get character length of 6 for string Julia and 7 for the string $Julia when they are read from the file. I checked the text file by turning on white spaces and there are none. What am i doing wrong?

like image 621
user1234 Avatar asked Apr 26 '17 13:04

user1234


People also ask

How do you replace characters in strings in Julia?

The replace() is an inbuilt function in julia that is used to replace a word or character with the specified string or character. Parameters: s::AbstractString: Specified string. pattern=>Word: Pattern is searched from the given sentence and then that pattern is replaced with the word.

How do you know if strings are equal in Julia?

The cmp() is an inbuilt function in julia which is used to return 0 if the both specified strings are having the same length and the character at each index is the same in both strings, return -1 if a is a prefix of b, or if a comes before b in alphabetical order and return 1 if b is a prefix of a, or if b comes before ...

How do I concatenate strings in Julia?

Using '*' operator It is used to concatenate different strings and/or characters into a single string. We can concatenate two or more strings in Julia using * operator.

What is SubString in Julia?

The SubString() is an inbuilt function in julia which is used to return a part of the specified parent string s within range i:j or r, where i, j and r is given as the parameter. Syntax: SubString(string::AbstractString, i::Integer, j::Integer)


1 Answers

The issue is that the strings returned by eachline contain a newline character at the end.

You can use chomp to remove it:

julia> first(eachline("julia.txt"))
"Julia\n"

julia> chomp(first(eachline("julia.txt")))
"Julia"

Also, you can simply use == instead of cmp to test whether two strings are equal. Both use a ccall to memcmp but == only does that for strings of equal length and is thus probably faster.

like image 199
tim Avatar answered Sep 25 '22 00:09

tim