The key thing to remember about Java strings is that they are immutable objects representing text as a sequence of Unicode characters (letters, digits, punctuation, etc.).
Double quotes are used to define a String
instance:
String fruit = "Apple";
Manipulating a string can be done using method of class String
.
As string values can never change after having been defined, all string manipulation methods will return a new string.
A string is delimited by double quote ("
) characters.
Some special characters need escaping using the backslash (\
) character.
Characters to be escaped in Java:
"
\
String escaped = "c:\\test.txt";
// => c:\test.txt
Finally, there are many ways to concatenate a string.
The simplest one is the +
operator:
String name = "Jane";
"Hello " + name + "!";
// => "Hello Jane!"
For any string formatting more complex than simple concatenation, String.format
method is preferred.
String name = "Jane";
String.format("Hello %s!",name);
// => "Hello Jane!"
Other possibilities are: