Reputation: 17
I wonder why the double quotation marks is not shown in the actual output - just after the equal sign:
String word = "" + c1 + c2 + "ll";
The full code as follows:
public class InstantMethodsIndexOf
{
public void start()
{
String greeting = new String ("Hello World");
int position = greeting.indexOf('r');
char c1 = greeting.charAt(position + 2);
char c2 = greeting.charAt(position - 1);
**String word = "" + c1 + c2 + "ll";**
System.out.println(word);
}
}
Upvotes: 0
Views: 2101
Reputation: 5410
When you pass ""
to a String
you are passing an empty String
. You need to escape the quotation with a back slash if you want to print them.
Example:
String word = "\"" + c1 + c2 + "ll\"";
then System.out.println(word)
will print:
"Hell"
As you can see I am escaping one double quotation at the beginning and another at the end
(Assuming c1 == 'H'
and c2 == 'e'
)
Upvotes: 4
Reputation: 201419
This code
String greeting = "Hello World"; // <-- no need for new String()
int position = greeting.indexOf('r'); // <-- 8
char c1 = greeting.charAt(position + 2); // <-- 'd'
char c2 = greeting.charAt(position - 1); // <-- 'o'
String word = "" + c1 + c2 + "ll"; // <-- "" + 'd' + 'o' + "ll"
The empty String ""
is used to coerce the arithmetic to a String, so it could also be written as
StringBuilder sb = new StringBuilder();
sb.append(c1).append(c2).append("ll");
String word = sb.toString();
or
StringBuilder sb = new StringBuilder("ll");
sb.insert(0, c2);
sb.insert(0, c1);
String word = sb.toString();
If you wanted to include double quotes in your word
, your could escape them with a \\
or use a character -
char c1 = greeting.charAt(position + 2); // <-- 'd'
char c2 = greeting.charAt(position - 1); // <-- 'o'
String word = "\"" + c1 + c2 + "ll\""; // <-- "\"" + 'd' + 'o' + "ll\""
or
String word = "" + '"' + c1 + c2 + "ll" + '"';
Upvotes: 1
Reputation: 82
The quotation mark does not appear because you have none being printed. What you have is an empty string being concatenated with other contents.
If you need the quotation mark, then you shoud do the following:
String word = "\"" + c1 + c2 + "ll";
Upvotes: 2
Reputation: 22972
I wonder why the double quotation marks is not shown in the actual output - just after the equal sign:
In java String
represented by the use of double quotes means the data between double quotes is considered as String
value but if you want to include double quotes you have to use escape
character \"
.
Moreover I suggest you to use StringBuilder
and append your characters and String
into it and use toString
to print.
String str="ABC";//So value of String literal is ABC not "ABC"
String empty="";//is just empty but NOT Null
String quote="\"";//Here String has value " (One Double Quote)
Upvotes: 1
Reputation: 38521
I wonder why the double quotation marks is not shown in the actual output - just after the equal sign:
String word = "" + c1 + c2 + "ll";
You are declaring a String
that concatenates:
""
c1
c2
"ll"
To show the quotes and make the code easier to read, try:
String word = '\u0022' + c1 + c2 + "ll"
which uses the unicode character value to print the double quote
Upvotes: 1
Reputation: 6437
It's a way to let Java know that it will be a string straight from the beginning, since ""
is a String object of an empty string.
In your code, it doesn't really look useful. But following is an example where it would be:
int a=10, b=20;
String word = a + b + "he"; // word = "30he"
String word2 = "" + a + b + "he"; // word2 = "1020he"
Upvotes: 1