简体   繁体   中英

Ruby split a string using two different delimiters

I want to split a string using "+" or "-" as delimiter.

  • test.split(/-/) will split by "-".
  • test.split(/\\+/) will split by "+".

I need to check for both of them.

只需使用一个字符类,然后在其中添加+-

puts test.split(/[+-]/);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM