【问题标题】:Split string with special character "|" [duplicate]用特殊字符“|”分割字符串[复制]
【发布时间】:2019-03-19 22:27:59
【问题描述】:

我正在从包含此字符“|”的 API 检索字符串。我想用“|”分割字符串我尝试了几个选项,但都失败了。

String response="The general String | Yesterday"; 
String splitResponse = response.split("|");
// also tried this 
reponse.split("(?<=|)"); //no success

【问题讨论】:

  • 试试split("\\|")
  • split() 将返回 String[] 而不是 String 对象。

标签: java android


【解决方案1】:

要使用'|' 进行拆分,您需要使用以下内容删除转义字符。

String response="The general String | Yesterday"; 
    String []splitResponse = response.split("\\|");
    for(int i=0;i<splitResponse.length;i++) {
        System.out.println(splitResponse[i]);
    }

Split 方法在REGEX 上拆分您的字符串。

【讨论】:

    猜你喜欢
    • 2019-11-03
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    • 2022-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-11
    相关资源
    最近更新 更多