如何在如下所示的文本文件中删除较短的字符串:
987987
9879872938472
987234
234987234987
32423
我希望能够删除少于 5 个字符的字符串。我想知道如何在使用 Java 方面做到这一点。
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
}
我假设你想要这样的东西。
// 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.
}
}