java分隔符替换新行

时间:2012-06-09 17:26:48

标签: android split substring delimited-text

这是一个输入的场景

String in = "<ENTER>title=Java-Samples<ENTER>" + 
                        "<ENTER>author=Emiley J<ENTER>" + 
                        "<ENTER>publisher=java-samples.com<ENTER>" + 
                        "<ENTER>copyright=2007<ENTER>" +
                        "<ENTER>cool beans<ENTER>";

PROCESSS

    in=in.substring(in.indexOf("<ENTER>")+7,in.lastIndexOf("<ENTER>"));
    String[] mSplitted= in.replaceAll("<ENTER><ENTER>", "<ENTER>").split("<ENTER>");
    String mFinal="";

    for(int i=0;i<mSplitted.length;i++)
    {
       System.out.println("values: "+mSplitted[i]);
       mFinal= mFinal+ mSplitted[i];
    }
    System.out.println(mFinal);

输出

title = Java-Samplesauthor = Emiley Jpublisher = java-samples.comcopyright = 2007cool beans

Senario 2

输入

        String in = "What is the output of: <ENTER><ENTER>echo 6 % 4;";

过程

    in=in.substring(in.indexOf("<ENTER>")+7,in.lastIndexOf("<ENTER>"));
    String[] mSplitted= in.replaceAll("<ENTER><ENTER>", "<ENTER>").split("<ENTER>");
    String mFinal="";

    for(int i=0;i<mSplitted.length;i++)
    {
      // System.out.println("values: "+mSplitted[i]);
       mFinal= mFinal+ mSplitted[i];
    }
    System.out.println(mFinal);

输出 没有

我希望输出在使用时添加新行

1 个答案:

答案 0 :(得分:1)

仍然不是100%的问题,但是如何:

String mFinal = in.replaceAll("<ENTER><ENTER>", "\n").replaceAll("<ENTER>", "");