Noona
Noona

Reputation: 11

How to delete symbols from a string in erlang

I need to ignore/delete all the symbols in a string (read from a file, but that doesnt matter). How do i do it? This is what i have so far

Words = string:to_lower(loadFile(FileToLoad)),
TokenList1 = (string:tokens(Words," \r\n,.-")),

Upvotes: 1

Views: 223

Answers (2)

Máté
Máté

Reputation: 2345

You can use the re module's replace/4 function, where you replace all non-alphabet characters with empty string. You can change the second argument there to any whatever you don't what to keep in your final string.

S = "@1a#2b*c3d%+".
re:replace(S, "[^a-zA-Z]", "", [global, {return, list}]).
% Returns "abcd"

Pay attention to the options at the end as well, these options return a list in the end (can return binary if needed), and apply the replace globally not just on the first occurrence.

Upvotes: 1

choroba
choroba

Reputation: 241768

I tried to use list comprehension:

S = "@1a#2b*c3d%+".
WithoutSymbols = [X || X <- S, Y <- lists:seq($a,$z), X =:= Y].

WithoutSymbols becomes abcd.

Upvotes: 1

Related Questions