Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a string in Julia?

Tags:

julia

I have the following line read in from my text file: "1200, 1200" and I want to split the string on the ", " such that I can access the raw numbers. How can I do that in Julia?

like image 706
logankilpatrick Avatar asked Jan 25 '23 05:01

logankilpatrick


1 Answers

Check out this comprehensive article on Splitting Strings in Julia.

Julia has a simple split function which takes in two arguments. The first is the string you want to split and the second is the delimiter (the thing you want to split on/by). Both parameters that are passed to the split function should be strings.

In this example:

data = readlines("numbers.txt") # Returns a one dimensional array of strings. 
xmin, ymin = split(data[1], ", ") # data[1] indexs into that 1-D array to get the string

we read in all of the data from a text file which is just simple "1200, 1400".

We can then use the split function to separate the two numbers into "xmin" and "ymin". In this example, after the code is run, "xmin" will be equal to 1200 and "ymin" will be equal to 1400.

Read more about the split function in the Julia docs.

like image 90
logankilpatrick Avatar answered Feb 07 '23 17:02

logankilpatrick