2012-09-21 57 views
0

怎麼能在我看起來像這樣的文本文件刪除短字符串:刪除文本文件中短串

987987 
9879872938472 
987234 
234987234987 
32423 

我希望能夠刪除少於5個字符字符串。我想知道如何在使用Java方面做到這一點。

+0

*「(分開行)」*使用代碼格式。或者更詳細地說。請使用代碼格式設置代碼,輸入/輸出和結構化文檔,如HTML或XML。爲此,請選擇樣本並單擊郵件發佈/編輯表單上方的「{}」按鈕。 –

+0

首先,你需要'拆分'字符串,然後你需要檢查每個字符串的'length',看它是否小於5(或者<<)... – bdares

回答

1
String string="987987 9879872938472 987234 234987234987 32423"; 
    String[] splitString=string.split(" "); 

    for(int i=0;i<splitString.length;i++){ 
     if(splitString[i].length()<5){ 
      continue; 
     } 
     //write to file 
    } 
0

我假設你想要這樣的東西。

// to read in the file  
Scanner sc = new Scanner(new File("file")); 

// read in each line and check to see if they are greater 
// than 5 characters in length 
while sc.hasNextLine(){ 
    String line = sc.nextLine().trim(); 
    if(line.length() >= 5) { 
     System.out.println(line); 
     // or code here to output to some file. 
    } 
}