Reputation: 73
My application flow is like i need to create a fixed length message and send it to end consumer over mqs.
AAA
BBB
CCC
DDD
Needs to be converted to AAABBBCCCDDD
and send to end application over mqs
for this i am using below code
String response = "AAA
BBB
CCC
DDD";
response.replaceAll(System.getProperty("line.separator"), "");
response = AAABBBCCCDDD
Everything is good so far, but when the message reach to end system they are complaining about a space after each line and when checked via hex it looks like a 0D character is getting inserted at place of next line
AAA BBB CCC DDD -> This is how they are received
AAA0DBBB0DCCC0DDDD -> Hex enabled Hex’0D’ (I think it’s more like delimiter, EOL or something)
Can someone suggest how can i get rid of these characters which are getting added?
Upvotes: 3
Views: 3738
Reputation: 73
responseTransactions.replaceAll("\r?\n", "");
this worked for me and the end systems have also confirmed that now they are not receiving any spaces or 0D in the message.
Upvotes: -1
Reputation: 425033
Use the any linebreak regex \R
:
response.replaceAll("\\R", "");
According to the documentation:
\R
Any Unicode linebreak sequence, is equivalent to
\u000D\u000A|[\u000A\u000B\u000C\u000D\u0085\u2028\u2029]
Upvotes: 3
Reputation: 12478
The ASCII code 0x0d
is CR
(Carriage Return).
When a string includes CR+LF
s as line separators (e.g. on Windows) and your system uses LF
s as line separators (e.g. on Unix descendants), System.getProperty("line.separator")
just gives you LF
. So your code removes only LF
s let alone CR
s.
If you just get rid of CR
or LF
, below may be sufficient.
response.replaceAll("[\\r\\n]", "");
Upvotes: 4
Reputation: 521239
If you want to remove the whitespace separators in your input, retaining all non whitespace characters, then a simple replace all should suffice:
String response = "AAA\nBBB\nCCC\nDDD";
String output = response.replaceAll("\\s+", "");
System.out.println(output); // AAABBBCCCDDD
Upvotes: 1