How to display \ and " in Java? [duplicate] - java

This question already has answers here:
What are all the escape characters?
(5 answers)
Closed 2 years ago.
How do you display the characters \ and "?
One backslash (\) +and + " (That's the needed output)
I got smth near but without " .
The answer on a website shows inoperative answer...
System.out.println('\' and '\"');

I'm presuming this is Java
//this code
System.out.println("Backslash is \\ and quote is \". The end.");
//will print this string into the console:
Backslash is \ and quote is ". The end.
Because \ is used to turn normal characters like n or t into special characters like NEWLINE \n or TAB \t. If you want to print out a backslash, you can't just write a single backslash on its own in a string because Java will then look at the next character after the backslash to know what to do. Because java needs another character after a backslash, it's a rule of the language that if you want to actually print a backslash you have to put a second backslash after the first. If you wanted to print two backslashes in succession, you would have to write FOUR backslashes in succession into your code.
//this code
System.out.println("Double Backslash is \\\\. The end.");
//will print this string into the console:
Double Backslash is \\. The end.
Because " is used to start and stop strings in code, you also need a way to indicate to Java that "i'm going to write a quote but it's to be printed literally, it doesn't stop the string", and for that you precede the " with a backslash like \"

#Caius Jard gace you the way to do it, just escape the character.
System.out.println("\\" + and + "\""); // and is a variable

Related

In java I how do I escape a * character [duplicate]

This question already has answers here:
Java doesn't work with regex \s, says: invalid escape sequence
(3 answers)
Closed 3 years ago.
I have a string
String a = "dcvdk*vmfdkvm*bmkjfnb*";
I want to replace the * character by space
I tried a.replaceAll("\*", " ");
But it is giving error as invalid escape sequence.
Can you please tell me how can I achieve this?
Escape the escape character:
"\\*"
Alternatively, just use replace, which treats the arguments as literals, not regexes:
a.replace("*", " ")
Or, as Aniket Sahrawat points out, you can use the char overload in this case:
a.replace('*', ' ')
Remember that the backslash have a special meaning in strings, and you need to escape the backslash itself to get an actual backslash:
a.replaceAll("\\*", " ");

Java Regex to replaceFirst occurence of (File.Separator or forwardslash(/) or backslash(\\)) in String [duplicate]

This question already has answers here:
Escaping special characters in Java Regular Expressions
(7 answers)
Closed 6 years ago.
I want to do what the title says , although i know how it can be done using the code below:
(Where local is a variable which represents a path):
String path = ( local.startsWith(File.separator) || local.startsWith("/") || local.startsWith("\\"))
? local.substring(File.separator.length(), local.length())
: local;
I need to convert the above to regex expression so i am using:
path = local.replaceFirst("[" + File.separator + "/\\]", "");
Coming from xml schema where i was using regex expressions and looking on tutorials here it seems to me that it must work but it doens't at all, i get this error ->:
java.util.regex.PatternSyntaxException: Unclosed character class near index 4
[\/\]
^
If i change the code to the below,it works:
local.replaceFirst("[" + File.separator + "/\\Q \\ \\E]", "");
Here is saying that \ is a special character but:
There are two ways to force a metacharacter to be treated as an ordinary character:
precede the metacharacter with a backslash, or
enclose it within \Q (which starts the quote) and \E (which ends it).
This question was also read : Forward slash in Java Regex
Well the error was :
By simply using \\ it was replaced by \ so i had only the special character.
The solution is to use \\\\ so it is replaced by \\ and the first \ treats the second \ as a character and not a special character.
Leaving this solution here in case somebody has the same problem..

How can i show line separator sign in console? [duplicate]

This question already has answers here:
How do I print escape characters in Java?
(8 answers)
Closed 7 years ago.
How can I print to console system sign for line separator like \n or \r;
I know that System.getProperty("line.separator") returns the line separator and it breaks the line but I'd like to show what sign it is. Like \n or \r.
I want console result to be:
line separator is: \n
I have this code:
System.out.println("Line separator: " + System.getProperty("line.separator"));
It Shows
Line separator:
and then breaks the line. I would like a function to show me a sign (\n or \r).
Simply replace returned line separators (\n or \r) with strings representing \ and n or r. To create \ character you need to escape it with another \ (since \ is special in String literals - it can be used for instance to create some special characters like tab \t, or characters using their hexadecimal form \uFFFF, or simply to escape itself).
System.out.println("Line separator: " + System.getProperty("line.separator")
.replace("\r", "\\r")//replace `\r` literal with `\` and `r`
.replace("\n", "\\n"));//same about `\n`
which in my case prints
Line separator: \r\n

System.out.println ("\"\"\\\\\"\""); [duplicate]

This question already has answers here:
What is the backslash character (\\)?
(6 answers)
Closed 7 years ago.
Why does this string print only ""\\""? Does the backslash do something to the string? Please explain the function of the backslash. All I know is that it is the escape character, but I don't understand why it does this to strings.
The backslash '\' can be used in a String to add characters that would otherwise be illegal (e.g. " and ') or have another meaning (e.g. t, b, n, r, f and \). for your particular example :
The first 2 backslashes are escaping the double quotes. So \"\" is printed as ""
The next backslashes are escaping the backslashes that immediately follow so \\\\ is printed as \\
The last 2 backslashes behave as the first 2 escaping the quotes so \"\" is printed as ""
The Backslash is the escape character, used to encode special things like " in your string (which you normally couldn't use, because they'd mark the end of a string). You should read up on "String literals" in the official Java documentation or the book you read to learn Java.

In Java, why does System.out.println("\" \\"); gives " \ as output

In Java when i run System.out.println("\" \\");
I get output as :
" \
Can you please explain in detail, why this is happening?
Because you escape double quotes ("") with a backslash (\) and also a backslash with a backslash.
backslash is a special character in JAVA and many other programming languages, one of its use is to escape characters in certain situation.
For example:
If you want to print a string containing double quotes like: How are you "Bob" ?
Printing this using System.out.println("How are you "Bob" ?"); will not work because you are closing the quotes just before the word Bob. Therefore, a character was used to deal with such situation so one can print double quotes inside a string:
System.out.println("How are you \"Bob\" ?");
Moreover, since we've agreed above that \ escapes the double quotes, if you want to print a single backslash inside a string, doing this System.out.println("\"); will open the string but will escape the second double quotes which will result in an error because the string was not closed. To fix this, you need to escape the backslash like this: System.out.println("\");
Other interesting uses of \:
\n character to return to a new line
\t character to insert a tab
More about escape character can be found on Wikipedia
System.out.println("\" \");
System.out.println(" --> String Open
\" --> Double Quote character escaped using backslash
\\ --> Backslash itself as a character escaped using backslash
"); --> String Close
will give you output as "\
For the list of escaped characters, You can find that here.
\t Insert a tab in the text at this point.
\b Insert a backspace in the text at this point.
\n Insert a newline in the text at this point.
\r Insert a carriage return in the text at this point.
\f Insert a formfeed in the text at this point.
\' Insert a single quote character in the text at this point.
\" Insert a double quote character in the text at this point.
\\ Insert a backslash character in the text at this point.
Yes. You are escaping two characters,
String s = "\" \\";
uses the single back-slash to escape first the double quote and then a backslash. So you get,
" \
You might also try
System.out.println(s.length());
Which would tell you "3". Because you have a String of '"', ' ' and '\'
Escape Sequences are explained in The Java Tutorial: Characters, which also allows Unicode characters,
System.out.println("\u03A9");
Will output a one character String that equals
Ω
In addition to comments of my precursors, you can check it in Oracle's Java Tutorial, list of escape sequences.
http://docs.oracle.com/javase/tutorial/java/data/characters.html
This is because when you put a \ before a special character in java, \ tells the JVM that it is not a special character, rather it is a part of a String.
So in your case, when you put a \ before " , it prints a double quote(") and when you again put \\ , it prints a slash (\).
If you want to know more about this, you can go through the inside of Java and how the special characters are handled in java.
Hope it helps.
These are called escape sequences. All the escape sequences start with \ (backward slash) character (e.g., \n, \t etc.,). Here \n, \t has special meaning to Java like line break and tab space respectively. Similarly " (double quote) has a special meaning saying that termination of string literals in Java. Instead of making " as a string literal terminator, we need to tell java compiler to treat it as a special sequence. Hence we use these escape sequences like \\ (for backward slash), \' (single quote), \r (carriage return) etc.,
Thanks,
JK
The standard definition according to oracle is as follows:
A character preceded by a backslash \ is an escape sequence and has special meaning to the compiler.The following table shows the Java escape sequences
EscapeSequence Description
\" Insert a double quote character in the text at this point.
\\ Insert a backslash character in the text at this point.
By following the above description in our case for System.out.println("\" \\"),
\" would be replaced with a double " quote character and
\\ would be replaced with a (single backslash) \ character.
Hence output printed will be " \
Hope this helps.

Categories

Resources