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.
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?
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.
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.
Try this:
First, you should split your
String
into aString[]
on line breaks usingString.split(String)
. Line breaks are usually'\n'
but to be safe, you can get the system line separator usingSystem.getProperty("line.separator");
.The code for splitting the
String
into aString[]
on line breaks can now beWith the String split into lines, you can now check if each line starts with
*
usingString.startsWith(String prefix)
, then make it an empty string if it does.Now all you have left to do is to combine your
String[]
back into a singleString