集册 Java实例教程 使用split方法根据与正则表达式匹配的定界符分割字符串

使用split方法根据与正则表达式匹配的定界符分割字符串

欢马劈雪     最近更新时间:2020-01-02 10:19:05

404
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
字符串被拆分为带有冒号,分号,竖线或制表符的单词。

public class Main {/*n o w  j a v a  . c o m 提供*/

  public static void main(String[] args) {

    String s = "One:Two;Three|Four\tFive";

    String regex = "[:;|\\t]";

    String strings[] = s.split(regex);

    for (String word : strings)

      System.out.println(word);

  }


}