Search code examples
javaline-breaksstringbuffer

Java, remove text and the line break in a stringBuffer


i have this text coming in to a string buffer,

857,9/25/14
REEL SCAN,BASIS WT
,0.7600,66067.3,50212.4,61.0
,49.572,-0.3720,0.0000
,111.89,108.28,94.72,106.23
,99.82,83.41

after running this

StringBuffer stringBuffer = new StringBuffer();
                String line;
                while ((line = bufferedReader.readLine()) != null) {

                    line = line.replace("REEL SCAN" + "," + "BASIS WT", "");
                    stringBuffer.append(line.trim());
                    stringBuffer.append("\n");
                }

i get this

857,9/25/14

,0.7600,66067.3,50212.4,61.0
,49.572,-0.3720,0.0000
,111.89,108.28,94.72,106.23
,99.82,83.41

how do i get rid of the blank line at the same time to make this

857,9/25/14
,0.7600,66067.3,50212.4,61.0
,49.572,-0.3720,0.0000
,111.89,108.28,94.72,106.23
,99.82,83.41

i have tried

line = line.replace("REEL SCAN" + "," + "BASIS WT" + "\n\n", "");

and other variations of that i found on here, but no luck. all i need is that second line completely gone, but it wont always be the second line.


Solution

  • Perhaps just skip that line entirely from the StringBuffer

    while ((line = bufferedReader.readLine()) != null) {
        if(line.contains("REEL SCAN,BASIS WT") {
            continue;
        }
        stringBuffer.append(line.trim());
        stringBuffer.append("\n");
    }