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

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.

Related

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

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

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 - remove quotes unless preceded by odd number of backslashes [duplicate]

This question already has answers here:
RegEx: Look-behind to avoid odd number of consecutive backslashes
(2 answers)
Closed 4 years ago.
I am using regex to strip quotes from a String value. These String values can contain escaped quotes but also escaped backslash characters.
I do not want to remove escaped quotes, only non-escaped quotes. However, the cases where escaped backslash characters are preceding a non-escaped quote is causing difficulty.
I want results like the following:
"value" -> value
'value' -> value
"\"value\"" -> \"value\" <-- contains escaped quotes
"value\" -> value\"
"value\\" -> value\\ <-- contains escaped backslash before non-escaped quote
"""val"ue\\\""" -> value\\\"
The following regex almost works for me, except that it is also stripping backslashes when there is an even number of them before a quote, when I only want to escape double and single quote characters.
(?<!\\\\)(?:\\\\{2})*[\"']
The problem occurs because you match those backslashes and they are removed. To keep them, capture these backslashes, and replace with $1 placeholder:
s.replaceAll("((?<!\\\\)(?:\\\\{2})*)[\"']", "$1")
See the regex demo.
The ((?<!\\\\)(?:\\\\{2})*) is now wrapped in (...) and you may refer to the value captured within this group by using $1 in the replacement pattern.

Identify and escape double quote, single quote and comma in Java [duplicate]

This question already has answers here:
Java, escaping (using) quotes in a regex
(2 answers)
Closed 8 years ago.
Sorry I could not find anything that works and hence I am asking this question. I have a basic string that could have feet("), inch(') or comma(,). All I want to do is identify those and escape them before further processing. Not having any luck with Regex, as you can tell I am not good with it yet. Need help. Thanks much!
Someone hinted at it in your comments, but its not entirely correct since String#replace only takes a single character, and you want to provide more than one for the replacement.
Say you have some function foo() that returns some regular expression that isn't escaped properly, with respect to the "\"" char, or the "\'" char:
String regexp = Bar.foo();
regexp = regexp.replaceAll("(\\\"|\\\')", "\\\\$0");
Pattern yourPatternName = Pattern.compile(regexp);
A little explanation: In Java, you need to escape certain special characters, such as n to mean newline ('\n'), or t to mean tab ('t'). Since you are already escaping them, they are no longer the literal characters '\' + 'n', for example. So, you need to escape them a second time, so that way when the regular expression is compiled, Pattern#compiler will see the two characters "\n" rather than the single character, which is the newline. To escape the '\n' character, you need to, of course, place in a new '\' character. Since we are doing a java.lang.String, we need to still escape that slash one more time.
As for the comma, you don't need to escape that. You only need to escape special characters. For a list of the ones that Pattern recognizes, you can check here:
http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

Replace/remove String between two character [duplicate]

This question already has answers here:
Removing a substring between two characters (java)
(3 answers)
Closed 9 years ago.
I want to remove a string that is between two characters and also the characters itself , lets say for example:
i want to replace all the occurrence of the string between "#?" and ";" and remove it with the characters.
From this
"this #?anystring; is #?anystring2jk; test"
To This
"this is test"
how could i do it in java ?
#computerish your answer executes with errors in Java. The modified version works.
myString.replaceAll("#\\?.*?;", "");
The reason being the ? should be escaped by 2 backslashes else the JVM compiler throws a runtime error illegal escape character. You escape ? characters using the backslash .However, the backslash character() is itself a special character, so you need to escape it as well with another backslash.
Use regex:
myString.replaceAll("#\?.*?;", "");
string.replaceAll(start+".*"+end, "")
is the easy starting point. You might have to deal with greediness of the regex operators, however.

Categories

Resources