2011-03-06 4 views
2

分割しようとする文字列があります。次のコードダウンロードしたtxtファイルを分割できません

lsSagor = "some text\n Some more text\n More text~Text again\n Text\n text~Some text ..." 

final String[] laList = lsSagor.split("~"); 

String[] laSaga = laList[0].split("\n"); 

が与える作品:

laSaga[0] => some text 

laSaga[1] => some more text 

laSaga[2] => More text 

をしかし、私はテキストファイルをダウンロードした場合、それは分割して失敗し、提供します:

laSaga[0] => "some text\n Some more text\n More text" 

だから、最初のスプリット作品だが、二番目ではありません。ここで

は、私は、ファイル

String lsSagor = getFileFromUrl(BASEURL+"/sagor.txt"); 

public static String getFileFromUrl(String url) 
{ 
    InputStream content = null; 
    try 
    { 
     HttpGet httpGet = new HttpGet(url); 
     HttpClient httpclient = new DefaultHttpClient(); 
     // Execute HTTP Get Request 
     HttpResponse response = httpclient.execute(httpGet); 
     content = response.getEntity().getContent(); 
    } 
    catch (Exception e) 
    { 
     //handle the exception ! 
    } 
    BufferedReader rd = new BufferedReader(new InputStreamReader(content), 4096); 
    String line; 
    StringBuilder sb = new StringBuilder(); 
    try { 
     while ((line = rd.readLine()) != null) { 
       sb.append(line); 
     } 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    try { 
     rd.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return sb.toString(); 


} 
+2

テキストファイルを1つのOSで作成し、別のOSにアップロードしました(WindowsとLinuxの間にあるようです)。そのため、改行が変換されました。 :) –

+0

前のコメントは正しいかもしれません。ちなみに、getFileFromURL関数では、ファイルから行を読み込んで文字列にマージし、もう一度分割したいと思う...ちょっと変です。 –

+0

@Sarwar:同じ問題を考えていた... – Nocturnal

答えて

0

をダウンロードするために使用するコードこんにちは、私は問題はのstring.Split()関数で
古い方法が、仕事:)

public static String[] splitString(String str, char separator) 
{ 
    String[] retVal = null; 

    int length = str.length(); 
    int size = 1; 

    int jIndx = 0; 
    int expressionLength = 0; 

    while ((jIndx = str.indexOf(separator, jIndx + 1)) != -1) 
    { 
     size++; 
    } 

    retVal = new String[size]; 
    jIndx = 0; 

    char[] charArray = str.toCharArray() ; 
    for (int index = 0; index < length; index++) 
    { 
     if (charArray[index] == separator) 
     { 
      retVal[jIndx] = str.substring(index - expressionLength, index); 
      jIndx++; 
      expressionLength = 0; 
     } 
     else 
      expressionLength++; 


     if (index + 1 == length) 
     { 
      retVal[jIndx] = str.substring(index + 1 - expressionLength, index + 1); 
     } 
    } 

    return retVal; 
} 
1

だと思うですFrom the documentation

あなたの文字列に分割する改行文字が含まれているとは思わないでしょう行う必要がありウルド

 while ((line = rd.readLine()) != null) { 
      sb.append(line); 
      sb.append("\n"); 
     } 

それを取得し、私はちょうどそれがた改行すべての最初の場所で読むために簡単な方法があると確信しています。

0

この(とても美しいではない)ソリューションです

lsSagor = "some text# Some more text# More text~Text again\n Text# text~Some text ..." 

String lsSagor = getFileFromUrl(BASEURL+"/sagor.txt"); 

final String[] laList = lsSagor.split("~"); 

    giAntalSagor = laList.length; 

    String[] laSaga = laList[0].split("#"); 

    final String[] guiLaList = new String[giAntalSagor]; 
    for (int i = 0; i < giAntalSagor; i++) 
    { 
     guiLaList[i] = laList[i].replaceAll("#", "\n"); 
    } 

guiLaListは私が欲しかった情報を取得するには「\ n」は、その他のリストlaListとレイアウトに使用されます。

関連する問題