2012-02-28 218 views
0

UPDATE:分割字符串[]

输出是前人的精力:

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........ 
+3

你说你想*输出是什么,但它实际输出? – 2012-02-28 05:16:17

+0

请发表你的输出。你得到了什么。 – joshua 2012-02-28 05:17:58

+0

为什么不能使用StringTokenizer代替分割。它有三个构造函数可以帮助你。 – developer 2012-02-28 05:18:01

回答

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]); 
    } 
} 
+0

@paislee。我在eclipse中编写了这个方法,编译并运行它。我看到了输出,这是问题中的预期,然后只发布在这里。你应该仔细看看。 – JProgrammer 2012-02-28 05:28:54

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... 
+0

+1通过查看输出,这看起来像问题的解决方案没有正确地问:) – Jayan 2012-02-28 05:27:59

+0

所以你说我在输出中缺少'[]'?我怎么没有正确地问? – issoa 2012-02-28 05:33:11

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]); 

     } 

} 
} 
0

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

多次使用分割。首先使用:::作为行/节标记。使用“< < <”歌曲入口分隔符。这是@保罗的答案,只是写这个答案,所以澄清这些步骤..