如何拆分一个字符串两次
我试图将字符串拆分两次
String example = response; String [] array = example.split(""); System.out.println(array[0]); String [] array2 = example.split(""); System.out.println(array2[2]);
我试图通过使用此代码(不成功)来实现这一点,但不是打印第一个分割我想保存它并继续进行第二次分割。 谁能解决这个问题,或者更好地解决两次分裂问题? 谢谢
这可能看起来很多……但你真的应该使用DOM解析器来操作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: 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 = "" + 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 = "Hello World "; String section = extractTagValue(example, "section"); String title = extractTagValue(example, "title"); System.out.printf("%s, %s\n", title, section); }
在执行时,输出
Hello, World