How to split a split a string twice

前端 未结 2 983
走了就别回头了
走了就别回头了 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 = "</" + tag + ">";
      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 = "<title>Hello</title><section>World</SECTION>";
      String section = extractTagValue(example,
          "section");
      String title = extractTagValue(example, "title");
    
      System.out.printf("%s, %s\n", title, section);
    }
    

    Which, when executed, outputs

    Hello, World
    
    0 讨论(0)
  • 2021-01-27 13:22

    This may seem like alot... but you should really be using a DOM parser for manipulating XML:

    import java.io.StringReader;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.InputSource;
    import org.xml.sax.SAXParseException;
    
    public class ExtractXML {
        public static void main(String argv[]) {
            DocumentBuilderFactory docBuilderFactory = null;
            DocumentBuilder docBuilder = null;
            Document doc = null;
            String rawStr = "Response: <section><title>Input interpretation</title>"
                    + "<sectioncontents>Ireland</sectioncontents></section>"
                    + "<section><title>Result</title>"
                    + "<sectioncontents>Michael D. Higgins</sectioncontents></section>";
            String docStr = rawStr.substring(rawStr.indexOf('<'));
            String answer = "";
    
            try {
                docBuilderFactory = DocumentBuilderFactory.newInstance();
                docBuilder = docBuilderFactory.newDocumentBuilder();
                doc = docBuilder.parse(new InputSource(new StringReader(docStr)));
            } catch (SAXParseException e) {
                System.out.println("Doc missing root node, adding and trying again...");
                docStr = String.format("<root>%s</root>", docStr);
    
                try {
                    doc = docBuilder.parse(new InputSource(new StringReader(docStr)));
                } catch (Exception e1) {
                    System.out.printf("Malformed XML: %s\n", e1.getMessage());
                    System.exit(0);
                }
            } catch (Exception e) {
                System.out.printf("Something went wrong: %s\n", e.getMessage());
            } finally {
                try {
                    // Normalize text representation:
                    doc.getDocumentElement().normalize();
    
                    NodeList titles = doc.getElementsByTagName("title");
    
                    for (int tIndex = 0; tIndex < titles.getLength(); tIndex++) {
                        Node node = titles.item(tIndex);
    
                        if (node.getTextContent().equals("Result")) {
                            Node parent = node.getParentNode();
                            NodeList children = parent.getChildNodes();
    
                            for (int cIndex = 0; cIndex < children.getLength(); cIndex++) {
                                Node child = children.item(cIndex);
                                if (child.getNodeName() == "sectioncontents") {
                                    answer = child.getTextContent();
                                }
                            }
                        }
                    }
    
                    System.out.printf("Answer: %s\n", answer);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

    Output:

    [Fatal Error] :1:98: The markup in the document following the root element must be well-formed.
    Doc missing root node, adding and trying again...
    Answer: Michael D. Higgins
    
    0 讨论(0)
提交回复
热议问题