Red
Red

Reputation: 115

A function that takes a string and gives you the first word separated by whitespace?

Example: get_first("wassup! man") = "wassup!"

This must be very simple but can someone point me to a right direction of the solution. Not seen examples online.

Upvotes: 2

Views: 239

Answers (2)

Odobenus Rosmarus
Odobenus Rosmarus

Reputation: 5998

1>string:split("wassup! man", " ").
["wassup!","man"]
2> hd(string:split("wassup! man", " ")).
"wassup!"

or you can use http://erlang.org/doc/man/string.html#lexemes-2

Upvotes: 5

Milad Shahidi
Milad Shahidi

Reputation: 713

I don't even know erlang, but Google is your friend! It would save yourself a lot of time if you try Googling your question before posting it here. Just try "erlang extract first word of string". The very first result will take you to this page: http://erlang.org/doc/man/string.html where you can see the sub_word function. But then it says that this is deprecated and you need to use nth_lexeme:

http://erlang.org/doc/man/string.html#nth_lexeme-3

Upvotes: 5

Related Questions