如何拆分字符串两次 [英] How to split a string twice

查看:86
本文介绍了如何拆分字符串两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一种更简单的提问方式。



我试图将字符串拆分两次

This might be a simpler way of asking the question.

I am trying to split a string twice

String example = response;
        String [] array = example.split("<section>");
        System.out.println(array[0]);

        String [] array2 = example.split("<title>");
        System.out.println(array2[2]);





我试图通过使用此代码来实现此目的(不成功),但不是打印第一个分割我想保存它继续进行第二次分割。

任何人都有解决这个问题或更好的方法来分裂字符串两次?



I was trying to achieve this by using this code (not successfully), but instead of printing the first split I would like to save it and carry on with the 2nd split.
Would anyone have a solution to this problem or better way of going about splitting a string twice?

推荐答案

请参阅我对我建议以不同方式分割的问题的评论。但看起来你的源字符串是XML,因此,在大多数情况下,你应该使用XML解析器并一次解析所有的源代码。您可以使用 javax.xml.xpath

http://docs.oracle.com/javase/7/docs/api/javax/xml/xpath/package-summary.html [ ^ ]。



有关XML解析的完整文档,请参阅: http://docs.oracle.com/cd/B28359_01/appdev.111/b28394/adx_j_parser.htm [ ^ ]。



祝你好运,祝新年好!

-SA
Please see my comment to the question where I advice to split in some different ways. But it looks like your source string is XML, so, in most cases, you should rather use an XML parser and parse all you have at once. You can use javax.xml.xpath:
http://docs.oracle.com/javase/7/docs/api/javax/xml/xpath/package-summary.html[^].

For complete documentation on XML parsing, please see: http://docs.oracle.com/cd/B28359_01/appdev.111/b28394/adx_j_parser.htm[^].

Good luck, best wishes in New Year!
—SA


OP写道:

试图将字符串拆分两次

trying to split a string twice

String example = response;
 String [] array = example.split("<section>");
 System.out.println(array[0]);

 String [] array2 = example.split("<title>");
 System.out.println(array2[2])








如果我理解你,你会尝试根据不同的条件将一些文本分成几部分。您希望在< section> 上显示文字,然后(其次)在< title> 上声明。

现在,看看我的伪代码(我对java不够熟悉):



If i understand you well, you're trying to split some text into parts based on different condition. Firtly, you want to devide text on "<section>" then (secondly) on "<title>" statement.
Now, have a look at mine pseudo-code (i'm not familiar with java enough):

String [] array = example.split("<section>");
System.out.println(array[0]);
//for each part in a first array
for(int i=0,array[].UBound(),i++)
{
    String [] array2 = array[i].split("<title>");
}





你看到了区别吗?



Do you see the difference?


这篇关于如何拆分字符串两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆