How to split a split a string twice

前端 未结 2 1004
走了就别回头了
走了就别回头了 2021-01-27 12:44

I am trying to split a string twice

String example = response;
    String [] array = example.split(\"
\"); System.out.println(array[0]); S
2条回答
  •  情深已故
    2021-01-27 13:07

    I really don't think you want use split here. I think you want to use something like

    // Extract a given tag value from an input txt.
    public static String extractTagValue(String txt,
        String tag) {
      if (tag == null || txt == null) {
        return "";
      }
      String lcText = txt.toLowerCase();
      tag = tag.trim().toLowerCase();
      String openTag = "<" + tag + ">";
      String closeTag = "";
      int pos1 = lcText.indexOf(openTag);
      if (pos1 > -1) {
        pos1 += openTag.length();
        int pos2 = lcText.indexOf(closeTag, pos1 + 1);
        if (pos2 > -1) {
          return txt.substring(pos1, pos2);
        }
      }
      return "";
    }
    
    public static void main(String[] args) {
      String example = "Hello
    World
    "; String section = extractTagValue(example, "section"); String title = extractTagValue(example, "title"); System.out.printf("%s, %s\n", title, section); }

    Which, when executed, outputs

    Hello, World
    

提交回复
热议问题