What is the purpose of Escape Sequence \' in java? - java

if I print ' (single quote) in the System.out.println() I can get exact output.
Like :
System.out.println("test'test");
output: test'test
What is the purpose of using \' escape sequence in java?
This also gives me the same output.
System.out.println("test\'test");
output: test'test
pls explain what is the main purpose of \' escape sequence in java

It's for use in character literals:
char c = '\'';
Without that, it would be painful to get a single apostrophe as a char.

This is useful for character literals stored in char.
Imagine you want a character constant to just hold a '. You could do:
public static final char SINGLE_QUOTE = '';
This won't work as it only is an empty character, but we want a single quote. Hence the escape character \'.
public static final char SINGLE_QUOTE = '\'';
If you print it on System.out.println, you'll see the difference.
For an exercise, try and take your exact example and see if you can print a double quote literal " without escaping it. You'll see it's not possible. You will have to escape it with \".
Reference: String literals and Escape Sequences for Character and String Literals.

Related

Check whether the string contains backslash or not?

In Java, \' denotes a single quotation mark (single quote) character, and \" denotes a double quotation mark (double quote) character.
So, String s = "I\'m a human."; works well.
However, String s = "I'm a human." does not make any compile errors, either.
Likewise, char c = '\"'; works, but char c = '"'; also works.
But I need to detect whether the string contains backslash or not:
"abcd'" does not contain backslash
"abcd\'" contains backslash.
I need to distinguish whether the string contains backslash or not.
You can't. The're called escape sequences for a reason. For example, \n once put in a String, cannot match a literal \ against itself. It's gone. All that's left, is a new-line.
Remember \ is used to escape a character. It itself doesn't remain a part of the String.
However, you can check for a literal \ by doing a simple contains like
String s = "abcd\\";
System.out.println(s.contains("\\"));
"abcd\" is not a valid string in java.
Here java treated \" as an escape sequence character("). So, if you want to put a backslash in a string then you need to use \ with escape sequence character.
String "abcd\'" has not contained backslash character. It has an escape sequence character \'.
Escape characters (also called escape sequences or escape codes) in
general are used to signal an alternative interpretation of a series
of characters. In Java, a character preceded by a backslash (\) is an
escape sequence and has special meaning to the java compiler.
When an escape sequence is encountered in a print statement, the
compiler interprets it accordingly. For example, if you want to put
quotes within quotes you must use the escape sequence, \", on the
interior quotes. To print the sentence: She said "Hello!" to me. you
should write:
System.out.println("She said \"Hello!\" to me.");
// Java program to illustrate to find a character
// in the string.
import java.io.*;
public static void main (String[] args)
{
// This is a string in which a character
// to be searched.
String str = "gee\\k";
// Returns index of first occurrence of character.
int firstIndex = str.indexOf('\\');
System.out.println("First occurrence of char '\\'" +
" is found at : " + firstIndex);
}
if(string.contains("\\")){
//TODO do your code here
}
\ is used as for escape sequence in Java.
If you want to print backslash in the string you just have to print "abcd\\".
For your example it would be:
boolean containsBs = "abcd\\".contains("\\");
When you are using Strings you do not need to use the escape character(backslash) for single quotation marks. Likewise when using char you do not need to escape the double quotation mark.
String use double quotation mark while chars use single quotation mark. You need to use the escape character for double quote in Strings and for simple quote in chars.
String ex="I'm an example";
String ex2="My name is \"example\"";
char c='"';
char c2='\'';
If you want to find out if a String contains backslash
String ex="abcd";
String ex2="abcd\\";
ex.contains("\\"); //false
ex.contains("\\"); //true
The first backslash is for escaping and the second is the character.

how to append a backquote in java string

Someone please help me how to append a backquote in a string in java.
I tried with String result="`"+value+"`";
I need to append the backquote before and after the string.
What is the correct way?
You should scape " using \"
Eg: "\""+value+"\"";
Ideone demo.
Edit: OP want to use backquote(```) so answer will be following and no need to scape.
"`"+value+"`"
Ideone demo.
When we use literal strings in Java, we use the quote (") character to indicate the beginning and ending of a string. For example, to declare a string called myString, we could this :-
String myString = "this is a string";
But what if we wanted to include a quote (") character WITHIN the string. We can use the \ character to indicate that we want to include a special character, and that the next character should be treated differently. \" indicates a quote character, not the termination of a string.
public static void main (String args[])
{
System.out.println ("If you need to 'quote' in Java");
System.out.println ("you can use single \' or double \" quote");
}
This allows us to include quote characters within a string.

Replace a special character with other special character within string

I want to replace a special character " with \" in string.
I tried str = str.replaceAll("\"","\\\");
But this doesnt work.
The closing quotes are missing in the 2nd parameter. Change to:
str = str.replaceAll("\"","\\\\\"");
Also see this example.
String.replaceAll() API:
Replaces each substring of this string that matches the given regular
expression with the given replacement.
An invocation of this method of the form str.replaceAll(regex, repl)
yields exactly the same result as the expression
Pattern.compile(regex).matcher(str).replaceAll(repl)
Note that backslashes () and dollar signs ($) in the replacement
string may cause the results to be different than if it were being
treated as a literal replacement string; see Matcher.replaceAll. Use
Matcher.quoteReplacement(java.lang.String) to suppress the special
meaning of these characters, if desired.
Btw, it is duplicated question.
You have to escape the \ by doubling it:\\
Code example:
String tt = "\\\\terte\\";
System.out.println(tt);
System.out.println(tt.replaceAll("\\\\", "|"));
This gives the following output:
\\terte\
||terte|

get regex to work in java

I have this regular expression pattern,
From: ["<][^>]*>
I need it to work in java and the double quotes is producing an error. When I try and escape it like so
From: [\"<][^>]*>
it does not produce the correct result. Does anyone know how to handle double quotes in java for regular expressions? Thanks
The \ character in Java String literals is a reserved escape character, so to add a regex escape character into a Java literal String object one must Escape the Escape :)
Eg. \\" will result in a regex of \" which will find double quote characters.
EDIT: One thing that I forgot was that the double quote character is also a reserved character for a Java string literalas well! Because of this the \ for the regex must be escaped as well as the " character.
The actual Java string literal will look like this String regex = "\\\"";

Is there a difference between single and double quotes in Java?

Is there a difference between single and double quotes in Java?
Use single quotes for literal chars, double quotes for literal Strings, like so:
char c = 'a';
String s = "hello";
They cannot be used any other way around (like in Python, for example).
A char is a single UTF-16 character, that is a letter, a digit, a punctuation mark, a tab, a space or something similar.
A char literal is either a single one character enclosed in single quote marks like this
char myCharacter = 'g';
or an escape sequence, or even a unicode escape sequence:
char a = '\t'; // Escape sequence: tab
char b = '\177' // Escape sequence, octal.
char c = '\u03a9' // Unicode escape sequence.
It is worth noting that Unicode escape sequences are processed very early during compilation and hence using '\u00A' will lead to a compiler error. For special symbols it is better to use escape sequences instead, i.e. '\n' instead of '\u00A' .
Double quotes being for String, you have to use a "double quote escape sequence" (\") inside strings where it would otherwise terminate the string.
For instance:
System.out.println("And then Jim said, \"Who's at the door?\"");
It isn't necessary to escape the double quote inside single quotes.
The following line is legal in Java:
char doublequote = '"';
Let's consider this lines of code (Java):
System.out.println("H"+"A"); //HA
System.out.println('H'+'a'); //169
First line is concatenation of H and A that will result in HA (String literal)
Second we are adding the values of two char that according to the ASCII Table H=72 and a=97 that means that we are adding 72+97 it's like ('H'+'a').
Let's consider another case where we would have:
System.out.println("A"+'N');//AN
In this case we are dealing with concatenation of String A and char N that will result in AN.
Single quote indicates character and double quote indicates string..
char c='c';
'c'-----> c is a character
String s="stackoverflow";
"stackoverflow"------> stackoverflow is a string(i.e collection if characters)

Categories