使用关键字将字符串拆分为多个部分

时间:2015-05-12 18:27:32

标签: java string

我有一个从.txt文件中读取的字符串,其中的值分为

Text first 
[section_name_1]
Text with values pattern1
...
[section_name_2]
Text with values pattern2

我需要在 section_name _#标记处拆分这些部分,并将它们添加到 String [] (数组的大小是固定的)。我的代码到现在并没有做出一些奇怪的输出:

//Code:
public static String[] parseFileToParams(File file)
    {
        String[] sections= {"[section_name_1]","[section_name_2]","[section_name_3]","[section_name_4]"};
        String[] params = new String[sections.length+1];
        StringBuilder sb = new StringBuilder();
        String decoded = parseFile(file);// Returns the Text from the file
        for(int i=0; i< sections.length;i++)
        {
            params[i]= decoded.split(sections[i])[1];
            sb.append(params[i]);
        }
        return params; 
    }
//For Test of the output
String[] textArray = BasicOsuParser.parseFileToParams(parseFile);
        for(int j = 0; j<textArray.length;j++)
        {
            sb.append(textArray[j]);
        }
        String text= sb.toString();
System.out.println (text); //Output: su f form formau fnull
// Obviously not how it should look like

感谢您的帮助!

4 个答案:

答案 0 :(得分:1)

试试这个:

String[] sections= {"[section_name_1]","[section_name_2]","[section_name_3]","[section_name_4]"};
        String textFromFile = "Text first [section_name_1] Text with values pattern1 [section_name_2] Text with values pattern2";
        int count = 0;
        for(int i = 0; i < sections.length; i++){
            if(textFromFile.contains(sections[i])){//Use this to tell how big the parms array will be.
                count++;
            }
            sections[i] = sections[i].replace("[", "\\[").replace("]", "\\]");//Removes the brackets from being delimiters.
        }
        String[] parms = new String[count+1];//Where the split items will go.
        int next = 0;//The next index for the parms array.
        for(String sec : sections){
            String split[] = textFromFile.split(sec);//Split the file's text by the sec
            if(split.length == 2){
                parms[next] = split[0];//Adds split to the parms
                next++;//Go to the next index for the parms.
                textFromFile = split[1];//Remove text which has just been added to the parms.
            }
        }
        parms[next] = textFromFile;//Add any text after the last split.
        for(String out : parms){
            System.out.println(out);//Output parms.
        }

这将按照您的要求进行操作并进行评论,以便您了解其工作原理。

答案 1 :(得分:1)

仅对文本中的一个分隔符使用split()并不是一个好主意。此方法尝试通过给定的正则表达式模式分隔文本,并且通常在文本中存在多个给定分隔符的情况下使用。你还应该在reqexp中屏蔽特殊符号,如'。','['等等。 read about patterns in java。在您的情况下,最好使用substring()indexOf()

public static String[] parseFileToParams(File file)
{
    String[] sections= {"[section_name_1]","[section_name_2]","[section_name_3]","[section_name_4]"};
    String[] params = new String[sections.length+1];
    String decoded = parseFile(file);// Returns the Text from the file
    int sectionStart = 0;
    for (int i = 0; i < sections.length; i++) {
        int sectionEnd = decoded.indexOf(sections[i], sectionStart);
        params[i] = decoded.substring(sectionStart, sectionEnd);
        sectionStart = sectionEnd + sections[i].length();
    }
    params[sections.length] = decoded.substring(sectionStart, decoded.length());
    return params; 
}

答案 2 :(得分:0)

params[i]= decoded.split(sections[i])[1];

这将在第一次出现部分[i]之后返回字符串,即不仅直到部分[i + 1]而是直到文件结尾。

答案 3 :(得分:0)

这个循环,     for(int i=0; i< sections.length;i++) { params[i]= decoded.split(sections[i])[1]; sb.append(params[i]); } return params; 重复地将解码分成两半,由给定的部分分开。然后你将整个下半部分追加到params。 例如,假装你想分割字符串&#34; abcdef&#34;沿&#34; a&#34;,&#34; b&#34;等 你会分开,并附加&#34; bcdef&#34;参数,然后沿b分开,并附加&#34; cdef&#34;对于params等,所以你会得到&#34; bcdefcdef ... f&#34;。 我想你想要做的是使用真正的正则表达式作为分隔符,如params = decoded.split([section_name_.])。查看http://www.tutorialspoint.com/java/java_string_split.htmhttps://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx 如果你想要