3

Say I have a string like this:

String message = "Hi \n Prakash";

I want to print messsage exactly the way it is rather than printing a newline for \n. So the desired output is:

Hi \n Prakash

and not

Hi
 Prakash

Please note that we are not allowed to modify the string message. How do I do this?

Edit: I've used \n just as an example. There could be similar characters like \t which could possibly be in my string.

prakasht
  • 448
  • 4
  • 16
  • 3
    The value of the string you have is exactly as the output you get, there is no text sequence `\n` stored in your string. What are you trying to do? – Progman Aug 03 '19 at 19:19
  • 1
    tl;dr: `\n != \\n` – Turing85 Aug 03 '19 at 19:20
  • I just want to know if it is possible to do the said task, just out of curiosity. Because I can't think of any way of printing that'll print `message` in just one line rather than two lines as it normally does. – prakasht Aug 03 '19 at 19:22
  • If you want to print the message in a single line, you can `message.split("\n")`, iterate over the resulting array and print it in one line, maybe separated by blanks. – Turing85 Aug 03 '19 at 19:24
  • For your edit: do the same thing as proposed in the answers, just for `\t` etc. – Andy Turner Aug 03 '19 at 19:27
  • 1
    "*Please note that we are not allowed to modify the string message.*" - You *CAN'T* modify a `String`. [`String`s are immutable](https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/lang/String.html) – Turing85 Aug 03 '19 at 19:28
  • Note that it's not actually possible to do what you are trying to do. For example, you can use `\u005cn` instead of `\n`, and it's indistinguishable to Java. – Andy Turner Aug 03 '19 at 19:31
  • @AndyTurner That's a very bold statement. If that's true, it's sad to know. – prakasht Aug 03 '19 at 19:34
  • I wonder if the Text Blocks feature ([JEP 355](https://openjdk.java.net/jeps/355)) in Java 13 will make a difference here. – Basil Bourque Aug 03 '19 at 19:38
  • @prakasht http://ideone.com/45UKmv – Andy Turner Aug 03 '19 at 19:48
  • 1
    What you want is essentially bytecode manipulation. As you should be aware, the sequence of characters your have in your source .java file is first turned into bytecode. This will replace your source of with the equivalent bytecode. At no time does the JVM ever see a literal slash in your string. It's too late to do this by the time it gets into the JVM. You would have to do this by detecting and replacing every possible escapable character. – Tibrogargan Aug 03 '19 at 19:48
  • @AndyTurner Funny thing, despite what you say is correct, handling just for `\n` in our code automatically handles `\u005cn`. Must be a feature of java to first convert `\u005cn` to `\n` before doing any further processing. – prakasht Aug 03 '19 at 19:53
  • 2
    @prakasht correct, it does, that's the reason you can't use `\u0022` in a string literal. My point is that you can't print the message exactly the way it appears in the source: `"Hi \u005cn Prakash"` cannot be distinguished from `"Hi \n Prakash"`, so you can't know which of those two forms to print. – Andy Turner Aug 03 '19 at 20:01
  • 1
    @prakasht `\u005c` is just an escape sequence for a literal `\ `, so `\u005cn` gets translated to `\n` during bytecode generation, which is again translated to a newline character before the JVM ever sees it. – Tibrogargan Aug 03 '19 at 20:04
  • @Tibrogargan looks like there's a ocean of things I'm yet to learn. – prakasht Aug 04 '19 at 02:29

2 Answers2

3

This can be done with the help of StringEscapeUtils class from Apache Commons Text.

import org.apache.commons.lang3.StringEscapeUtils;
String message = "Hi \n Prakash";
System.out.println(StringEscapeUtils.escapeJava(message));

The output is:

Hi \n Prakash

Thanks Dima for providing the link in answer.

prakasht
  • 448
  • 4
  • 16
0

You want to do escaping of special characters. If it is your code and you want to escape couple of strings then the best way to do it by adding an extra slash.

String message = "Hi \\n Prakash";

Output:

Hi \n Prakash

But if you want to read the text from a file and output it as it is with the special characters, then please refer to the following answer How do I escape a string in Java?

Dima
  • 420
  • 2
  • 17