java 从包含特定字符的字符串中删除行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28953054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:27:13  来源:igfitidea点击:

Removing lines from a string containing certain character

javastring

提问by Serpace

I am working a Java project to read a java class and extract all DOC comments into an HTML file. I am having trouble cleaning a string of the lines I don't need.

我正在处理一个 Java 项目来读取一个 Java 类并将所有 DOC 注释提取到一个 HTML 文件中。我在清理一串不需要的行时遇到了麻烦。

Lets say I have a string such as:

假设我有一个字符串,例如:

"/**
* Bla bla
*bla bla
*bla bla
*/

CODE
CODE
CODE

/**
* Bla bla
*bla bla
*bla bla
*/ "

I want to remove all the lines not starting with *.

我想删除所有不以*.

Is there any way I can do that?

有什么办法可以做到吗?

回答by ankh-morpork

First, you should split your Stringinto a String[]on line breaks using String.split(String). Line breaks are usually '\n'but to be safe, you can get the system line separator using System.getProperty("line.separator");.

首先,你应该将拆分StringString[]使用换行符String.split(String)。换行符通常是'\n'为了安全起见,您可以使用System.getProperty("line.separator");.

The code for splitting the Stringinto a String[]on line breaks can now be

用于将 拆分StringString[]换行符的代码现在可以是

String[] lines = originalString.split(System.getProperty("line.separator"));

With the String split into lines, you can now check if each line starts with *using String.startsWith(String prefix), then make it an empty string if it does.

将 String 拆分成行后,您现在可以检查每行是否以*using开头,如果是String.startsWith(String prefix),则将其设为空字符串。

for(int i=0;i<lines.length;i++){
    if(lines[i].startsWith("*")){
        lines[i]="";
    }
}

Now all you have left to do is to combine your String[]back into a single String

现在你剩下要做的就是将你的String[]背部组合成一个单一的String

StringBuilder finalStringBuilder= new StringBuilder("");
for(String s:lines){
   if(!s.equals("")){
       finalStringBuilder.append(s).append(System.getProperty("line.separator"));
    }
}
String finalString = finalStringBuilder.toString();

回答by Dermot Blair

Yes Java's String class has a startsWith() method that returns a boolean. You can iterate through each line in the file and check if each line starts with "*" and if it does delete it.

是的,Java 的 String 类有一个可以返回布尔值的 startsWith() 方法。您可以遍历文件中的每一行并检查每一行是否以“*”开头以及是否将其删除。

Try this:

试试这个:

File inputFile = new File("myFile.txt");
File tempFile = new File("myTempFile.txt");

BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

String linesToRemoveStartsWith = "*";
String currentLine;

while((currentLine = reader.readLine()) != null) {
    // trim newline when comparing with lineToRemove
    String trimmedLine = currentLine.trim();
    if(!trimmedLine.startsWith(linesToRemoveStartsWith )) continue;
    writer.write(currentLine + System.getProperty("line.separator"));
}
writer.close(); 
reader.close(); 
boolean successful = tempFile.renameTo(inputFile);

回答by mashaned

As far as I remember, you cannot delete lines from the text documents in-place in Java. So, you would have to copy the needed lines into a new file. Or, if you need to output the same file, just changed, you could read the data into memory, delete the file, create a new one with the same name and only output the needed lines. Although, frankly, for me it seems a bit silly.

据我所知,您不能在 Java 中就地删除文本文档中的行。因此,您必须将所需的行复制到新文件中。或者,如果您需要输出相同的文件,只是更改,您可以将数据读入内存,删除文件,创建一个同名的新文件,只输出所需的行。虽然,坦率地说,对我来说这似乎有点傻。