【问题标题】:How to split a string if any chracter is found in java如果在java中找到任何字符,如何拆分字符串
【发布时间】:2014-07-22 20:51:16
【问题描述】:

给定:

String one = "show ip interface brief | include 1234**\n Gi1/23.1234  xxx.225.xxx.106 YES manual up  up";

我要分手

Gi1/23.1234  xxx.225.xxx.106 YES manual up up

放入不同的令牌并删除

show ip interface brief | include  1234**

输出应该是:

String[] tokens = ["Gi1/23.1234", "xxx.225.xxx.106", "Yes", "manual", "up", "up"]

【问题讨论】:

  • 嗯……你说的。使用 String 类的 split() 方法
  • 您的帖子几乎无法阅读。无论如何,您是否尝试过任何方法,例如 String 类中的 split 方法?我们可以看看你的尝试吗?
  • 是的,我已经尝试过,但我无法从 Gil 开始拆分第二行。因为拆分方法不拆分“|” .请如果你能做任何帮助它真的很紧急
  • 您必须转义 regex 字符 "|"。示例:string.split("\\|")

标签: java regex string split tokenize


【解决方案1】:

你可以在这里同时使用String的方法replaceAllsplit

String one = "show ip interface brief | include 1234**\n Gi1/23.1234  xxx.225.xxx.106 YES manual up  up";
String[] tokens = one.replaceAll("[^\\n]+\\n\\s*", "").split("\\s+");
System.out.println(Arrays.toString(tokens));

输出

[Gi1/23.1234, xxx.225.xxx.106, YES, manual, up, up]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-29
    • 1970-01-01
    • 2019-03-03
    • 2012-07-31
    相关资源
    最近更新 更多