2
   char[] charArray = {'\n'};
   String str = String.valueOf(charArray);
   System.out.println(str);

So I want to obtain a string of the newline character that is in the array. However, with this code, the output is simply a blank space. How can I get String str= "\n" ?

2
  • clarify a bit you want to print \n on screen or replace new line character from input with \n on screen? Commented Nov 3, 2015 at 2:29
  • I simply want to print out "\n" not as actual whitespace but as if i was printing any other string like "hi" Commented Nov 3, 2015 at 2:31

1 Answer 1

2

'\n' is the encoding for a newline character. Perhaps you want:

String str = "\\n"

or perhaps you want

char[] charArray = {'\\','n'};
String str = String.valueOf(charArray)
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, that is correct. Going to edit that in the original post
So that means I can't use valueOf(charArray) because that immediately interprets the newline as whitespace (as it should)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.