Strange behavior with java strings

I have two string variables and a ticker. I am trying to print two lines on one line. It just won't work. I have tried so many different ways to do this. To rule out the possibility of an uninitialized string, I tried printing them on different lines ... this works.

This example works ... except that the output should be on the same line.

System.out.println(ticker); System.out.println(detail); 

And the result:

 IWM |0#0.0|0#0.0|0#-4252#386| GLD |0#0.0|0#0.0|0#-4704#818| 

When I try to put the output on one line in any of many ways, I get only a ticker ... the line with the details just does not print ... not for the console or for the file. Here are a few code examples that give the same result:

Attempt 1:

  System.out.println(ticker.concat(detail)); 

Attempt 2:

 System.out.println(ticker+detail); 

Attempt 3:

 StringBuffer sb = new StringBuffer(); sb.append(ticker); sb.append(detail); System.out.print(sb.toString()); 

Attempt 4:

 System.out.print(ticker); System.out.println(detail); 

In all of the above attempts, I get the following conclusion ... as if the part detail is ignored:

 GOLD BBL SI 

What can cause these symptoms? Is there a way to get two lines printed on one line?

+6
source share
2 answers

It may be better suited as a comment, but then I could not write the piece of code that I need to write.

Where do the strings come from? Are they from a file that may contain some odd control characters? If you are not creating String yourself, you should study them to look for inline vertical carriage returns or other weirdness. Do something similar for detail and ticker lines:

 for (int i=0; i<detail.length(); ++i) System.out.println((int) detail.charAt(i)); 

and see if you get anything in a range other than ASCII.

+7
source

Maybe the first line ends with the character "\ n", a newline character (line string) ('\ u000A')

+2
source

Source: https://habr.com/ru/post/905820/


All Articles