Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
556 views
in Technique[技术] by (71.8m points)

ruby - Split string by multiple delimiters

I want to split a string by whitespaces, , and ' using a single ruby command.

  1. word.split will split by white spaces;

  2. word.split(",") will split by ,;

  3. word.split("'") will split by '.

How to do all three at once?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
word = "Now is the,time for'all good people"
word.split(/[s,']/)
 => ["Now", "is", "the", "time", "for", "all", "good", "people"] 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...