Java Strings
Strings are used for storing text.
A String variable contains a collection of characters
surrounded by double quotes (""):
Example
Create a variable of type String and assign it a value:
String greeting = "Hello";
String Length
A String in Java is actually an object, which means it
contains methods that can perform certain operations on
strings.
For example, you can find the length of a string with
the length() method:
Example:
String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
[Link]("The length of the txt string is: " +
[Link]());
More String Methods
There are many string methods available in Java.
example:
[Link] toUpperCase() method converts a string to upper
case letters.
[Link] toLowerCase() method converts a string to lower
case letters.
Example:
String txt = "Hello World";
[Link]([Link]()); //Outputs
"HELLOWORLD"
[Link]([Link]()); // Outputs "hello world"
Finding a Character in a String
The indexOf() method returns the index (the position) of the
first occurrence of a specified text in a string (including
whitespace):
Example
String txt = "Please locate where 'locate' occurs!";
[Link]([Link]("locate")); // Outputs 7
You can use the charAt() method to access a character at a
specific position in a string:
Example
String txt = "Hello";
[Link]([Link](0)); // H
[Link]([Link](4)); // o
Comparing Strings
To compare two strings, you can use the equals() method:
Example:
String txt1 = "Hello";
String txt2 = "Hello";
String txt3 = "Greetings";
String txt4 = "Great things";
[Link]([Link](txt2)); // true
Removing Whitespace
The trim() method removes whitespace from the beginning and
the end of a string:
Example:
String txt = " Hello World ";
[Link]("Before: [" + txt + "]");
[Link]("After: [" + [Link]() + "]");
Result:
Before: [ Hello World ]
After: [Hello World]
String Concatenation
The + operator can be used between strings to combine them.
This is called concatenation:
Example:
String firstName = "John";
String lastName = "Doe";
[Link](firstName + " " + lastName);
Concatenation in Sentences
You can use string concatenation to build sentences with both
text and variables:
Example:
String name = "John";
int age = 25;
[Link]("My name is " + name + " and I am " + age
+ " years old.");
Result:
My name is John and I am 25 years old.
The concat() Method
You can also use the concat() method to concatenate strings:
Example:
String firstName = "John ";
String lastName = "Doe";
[Link]([Link](lastName));
You can also join more than two strings by
chaining concat() calls:
Example
String a = "Java ";
String b = "is ";
String c = "fun!";
String result = [Link](b).concat(c);
[Link](result);
Java Numbers and Strings
Adding Numbers and Strings
If you add two numbers, the result will be a number:
Example
int x = 10;
int y = 20;
int z = x + y; // z will be 30 (an integer/number)
If you add two strings, the result will be a string concatenation:
Example
String x = "10";
String y = "20";
String z = x + y; // z will be 1020 (a String)
If you add a number and a string, the result will be a string
concatenation:
Example
String x = "10";
int y = 20;
String z = x + y; // z will be 1020 (a String)
Java Special Characters
Strings - Special Characters
Because strings must be written within quotes, Java will
misunderstand this string, and generate an error:
String txt = "We are the so-called "Vikings" from the north.";
The solution to avoid this problem, is to use the backslash
escape character.
The backslash (\) escape character turns special characters into
string characters:
Escape Result Description
character
\' ' Single quote
\" " Double quote
\\ \ Backslash
The sequence \" inserts a double quote in a string:
Example
String txt = "We are the so-called \"Vikings\" from the north.";
The sequence \' inserts a single quote in a string:
Example
String txt = "It\'s alright.";
The sequence \\ inserts a single backslash in a string:
Example
String txt = "The character \\ is called backslash.";
Other common escape sequences that are valid in Java are:
Code Result
\n New Line
\t Tab
\b Backspace
\r Carriage Return
\f Form Feed