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)
Related
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.
I have a string which contains multiple unicode characters. I want to identify all these unicode characters, ex: \ uF06C, and replace it with a back slash and four hexa digits without "u" in it.
Example:
Source String: "add \uF06Cd1 Clause"
Result String: "add \F06Cd1 Clause"
How can achieve this in Java?
Edit:
Question in link Java Regex - How to replace a pattern or how to is different from this as my question deals with unicode character. Though it has multiple literals, it is considered as one single character by jvm and hence regex won't work.
The correct way to do this is using a regex to match the entire unicode definition and use group-replacement.
The regex to match the unicode-string:
A unicode-character looks like \uABCD, so \u, followed by a 4-character hexnumber string. Matching these can be done using
\\u[A-Fa-f\d]{4}
But there's a problem with this:
In a String like "just some \\uabcd arbitrary text" the \u would still get matched. So we need to make sure the \u is preceeded by an even number of \s:
(?<!\\)(\\\\)*\\u[A-Fa-f\d]{4}
Now as an output, we want a backslash followed by the hexnum-part. This can be done by group-replacement, so let's get start by grouping characters:
(?<!\\)(\\\\)*(\\u)([A-Fa-f\d]{4})
As a replacement we want all backlashes from the group that matches two backslashes, followed by a backslash and the hexnum-part of the unicode-literal:
$1\\$3
Now for the actual code:
String pattern = "(?<!\\\\)(\\\\\\\\)*(\\\\u)([A-Fa-f\\d]{4})";
String replace = "$1\\\\$3";
Matcher match = Pattern.compile(pattern).matcher(test);
String result = match.replaceAll(replace);
That's a lot of backslashes! Well, there's an issue with java, regex and backslash: backslashes need to be escaped in java and regex. So "\\\\" as a pattern-string in java matches one \ as regex-matched character.
EDIT:
On actual strings, the characters need to be filtered out and be replaced by their integer-representation:
StringBuilder sb = new StringBuilder();
for(char c : in.toCharArray())
if(c > 127)
sb.append("\\").append(String.format("%04x", (int) c));
else
sb.append(c);
This assumes by "unicode-character" you mean non-ASCII-characters. This code will print any ASCII-character as is and output all other characters as backslash followed by their unicode-code. The definition "unicode-character" is rather vague though, as char in java always represents unicode-characters. This approach preserves any control-chars like "\n", "\r", etc., which is why I chose it over other definitions.
Try using String.replaceAll() method
s = s.replaceAll("\u", "\");
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.
I read the following phrase in the Java language specification.
It is a compile-time error for the character following the SingleCharacter or
EscapeSequence to be other than a '.'
I am not able to understand what is the meaning of above line. Could someone please explain it with example.
What is says is basically: A compile time error will be generated for every character different than a ', that comes after the "character" itself. Where the "character" is the content in the form of a character (like: a, 0, \u0093) or an escape sequence (like: \\, \b, \n).
So, this will be wrong:
'aa', because the second a is not a single quote (').
'\\a', because the second character (the a) is not a single quote.
'a, because the character which comes after the "content" is not a quote (but probably a newline or a space).
Side note: This won't work either: char c = '\u0027';. Because that is the code point for a single quote, so it gets translated into: char c = ''';.
I guess this is about character literals. Another way to say this is: character literals must be enclosed by apostrophes, it is an error if you forget the second apostrophe.
Hence:
'a' // correct
'\007' // correct
'ab // wrong
In Java, you can define character variable as an escape sequences or single characters. Those should be surrounded by single quotes.
char ch = 'a';
// Unicode for uppercase Greek omega character
char uniChar = '\u039A';
More information and examples can be found in Java tutorial on Characters.
Does Java (or any other 3rd party lib) provide an API for replacing characters based on character code (within a known Charset of course) rather than a regex? For instance, to replace double quotes with single quotes in a given string, one might use:
String noDoubles = containsDoubles.replace("\"", "'");
However the UTF-8 character code for a double quote is U+0022. So is there anything that could search for instances of U+0022 characters and replace them with single quotes?
Also, not just asking about double/single quotes here, I'm talking about the character code lookup and replacement with any 2 characters.
Use the overloaded version - String#replace(char, char) which takes characters. So, you can use it like this:
String str = "aa \" bb \"";
str = str.replace('\u0022', '\'');
System.out.println(str); // aa ' bb '
Simply use the unicode literal:
// I'm using an unicode literal for "
String noDoubles = containsDoubles.replace('\u0022', '\'');
The above will work for any character, as long as you know its corresponding code.
You can also use a regex still. From the Javadoc:
\xhh The character with hexadecimal value 0xhh
\uhhhh The character with hexadecimal value 0xhhhh
Hence you could write this:
String noDoubles = containsDoubles.replace("\\u0022", "'");