I'm new to Julia and I was wondering if there were any built in functions for trimming string whitespace? I also want to check the length of the string which I know I can do with length(s) == 0
, but I wondered if there where other built in functions? Thanks!
I'm basically trying to find the Julia equivalent to the following MATLAB code:
line = strtrim(line);
if isempty(line), continue; end % Skip empty lines
For the start/end of a string you have
lstrip(string)
rstrip(string)
if you need to take everything out I recommend you use something like
a = "a b c d e f"
join(map(x -> isspace(a[x]) ? "" : a[x], 1:length(a)))
because sometimes you can get strings that include some weird whitespaces that wont match " "
or ' '
as is shown here
Edit
filter(x -> !isspace(x), a)
as suggested by Fengyang Wang, is even better
lstrip
for leading whitespace, rstrip
for trailing whitespace, strip
for both.
There is an isempty
function for julia as well:
isempty("")
>> true
Perhaps you should check out the Julia docs for other string-related functions (https://docs.julialang.org/en/stable/ & https://docs.julialang.org/en/stable/manual/strings/)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With