0

Before leaving a negative comment about me copying questions:
I've read the other stackoverflow question (Remove all blank spaces and empty lines) about this and tried out all of the suggestions but none worked for me.
I want to remove blank spaces in the following CSV:

<STX> somevalue;somevalue1;somevalue2 <CR><LF>

I need to remove the blank space between "STX" and "somevalue" and the blank space between "somevalue2" and "CR".

Thanks in advance.

Community
  • 1
  • 1
timoruether
  • 19
  • 1
  • 8
  • Use a regex to replace only the space(s) after `` and before `` but I let you write it, there are plenty of post about that – AxelH Apr 07 '17 at 10:23

2 Answers2

0

You can use replace:

String s1="<STX> somevalue;somevalue1;somevalue2 <CR><LF>";
String replaceString=s1.replace(' ','');//replaces all occurrences of ' ' to ''   
  • This is only works if `somevalue#` don't have spaces – AxelH Apr 07 '17 at 10:22
  • Using replace does work with astring, that's true, but my file is a .CSV. This was my first idea too, but I did not find a solution to convert a CSV to a String in java yet. – timoruether Apr 07 '17 at 10:22
  • @AxelH it's guaranteed that somevalue_n does not have any spaces. – timoruether Apr 07 '17 at 10:23
  • @timor, A csv is only a text file with a specific delimited character. So read the file, there is API for this. SO the question is `How to read a CSV?`. Also, specifiy that the are no space any other place then – AxelH Apr 07 '17 at 10:25
  • @timor https://www.mkyong.com/java/how-to-read-and-parse-csv-file-in-java/ – Nikos Vita Topiko Apr 07 '17 at 10:25
0

Try this, it works.

public class Test {

    public static void main(String[] args) {
        String str = "<STX> somevalue;somevalue1;somevalue2 <CR><LF>";
        str = str.replaceAll("\\s", "");
        System.out.println(str);
    }
}
Muhammad Iqbal
  • 144
  • 1
  • 1
  • 12