拆分字符串[]

时间:2012-02-28 05:14:27

标签: java arrays string

更新:

输出应该是:

chapter: Chapter 01
title: one
code: 111111

chapter: Chapter 02
title: two
code: 222222

chapter: Chapter 03
title: three
code: 333333

chapter: Chapter 04
title: four
code: 444444

chapter: Chapter 05
title: five
code: 555555

我有这个代码来分割字符串[]。我不确定问题是什么。请有人帮帮我吗?

String[] myArray = "Chapter 01<<<one<<<111111:::Chapter 02<<<two<<<222222:::Chapter 03<<<three<<<33333:::Chapter 04<<<four<<<4444:::Chapter 05<<<five<<<5555:::"

为了简单起见,我制作了假人String[]

for (int j = 0; j < myArray.length; j++)
{
String[] songs = myArray[j].split("\\<<<");

System.out.println("chapter: " + songs[0]);
System.out.println("title: " + songs[1]);
System.out.println("code: " + songs[2]);
} 

所以我想在结果中看到:

chapter: Chapter 01
title: one
code: 111111
............so on and so forth........

4 个答案:

答案 0 :(得分:3)

public void splitString(){
    String[] myArray = "Chapter 01<<<one<<<111111:::Chapter 02<<<two<<<222222:::Chapter 03<<<three<<<33333:::Chapter 04<<<four<<<4444:::Chapter 05<<<five<<<5555:::".split(":::");
    for (int j = 0; j < myArray.length; j++)
    {
        String[] songs = myArray[j].split("<<<");

        System.out.println("chapter: " + songs[0]);
        System.out.println("title: " + songs[1]);
        System.out.println("code: " + songs[2]);
    } 
}

答案 1 :(得分:2)

这有用吗:

public class Tmp {

  public static void main (String[] args) {

    String myArray = 
      "Chapter 01<<<one<<<111111:::Chapter 02<<<two<<<222222:::Chapter 03<<<three<<<33333";

    String[] chapters = myArray.split (":::");
    for (int i=0; i < chapters.length; i++) {
      System.out.println ("chapters[" + i + "]: " + chapters[i] + "...");

      String[] sections = chapters[i].split ("<<<");
      for (int j=0; j < sections.length; j++)
        System.out.println ("  sections[" + j + "]: " + sections[j] + "...");
    }
  }

}

java Tmp
chapters[0]: Chapter 01<<<one<<<111111...
  sections[0]: Chapter 01...
  sections[1]: one...
  sections[2]: 111111...
chapter[s1]: Chapter 02<<<two<<<222222...
  sections[0]: Chapter 02...
  sections[1]: two...
  sections[2]: 222222...
chapters[2]: Chapter 03<<<three<<<33333...
  sections[0]: Chapter 03...
  sections[1]: three...
  sections[2]: 33333...

答案 2 :(得分:1)

import java.util.StringTokenizer;

public class Stringss {

public static void main(String[] args){
    String myString = "Chapter 01<<<one<<<111111:::" +
            "Chapter 02<<<two<<<222222:::" +
            "Chapter 03<<<three<<<33333:::" +
            "Chapter 04<<<four<<<4444:::" +
            "Chapter 05<<<five<<<5555:::";

    StringTokenizer st = new StringTokenizer(myString,":::");
    String[] songs;
    while (st.hasMoreElements()) {
        String token = st.nextToken();
        songs = token.split("\\<<<");

        System.out.println("chapter: " + songs[0]);
        System.out.println("title: " + songs[1]);
        System.out.println("code: " + songs[2]);

        }

}
}

答案 3 :(得分:0)

您需要发布正确的问题 - 请参阅@Nirmal et all的评论。

多次使用拆分。首先使用:::作为行/节标记。使用“&lt;&lt;&lt;&lt;&lt;歌曲入口分隔符。这是@Paul的答案,只是写下这个答案,以澄清这些步骤..