Java Strings


Java Strings

Strings are used to store text.

String variable contains a set of characters surrounded by two quotes:


Example

Create a variable of type String and assign it a value:

String greeting = "Hello";
        


String Length

String in Java is actually an object, containing methods that can perform certain functions in a character unit. For example, the length of a character unit can be found in the form of a length():


Example
String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
System.out.println("The length of the txt string is: " + txt.length());
            


More String Methods

There are many character unit modes available, for example toUpperCase() and toLowerCase():


Example
String txt = "Hello World";
System.out.println(txt.toUpperCase());   // Outputs "HELLO WORLD"
System.out.println(txt.toLowerCase());   // Outputs "hello world"
            


Finding a Character in a String

The indexOf() method returns the index (location) for the first occurrence of a specific text in a series (including a white space):


Example
String txt = "Please locate where 'locate' occurs!";
System.out.println(txt.indexOf("locate")); // Outputs 7
            

Java calculates positions from zero.

0 is the first place in the series, 1 is the second, 2 is the third ...



String Concatenation

Operator + can be used between wires to connect them. This is called concatenation:


Example
String firstName = "John";
String lastName = "Doe";
System.out.println(firstName + " " + lastName);
            

Note that we have added blank text ("") to create space between firstName and printed surname.


You can also use the concat() method to connect two strings:


Example
String firstName = "John ";
 String lastName = "Doe";
System.out.println(firstName.concat(lastName));
            


Special Characters

Because character units have to be written between quotes, Java will not fully understand this series, and will produce an error:


String txt = "We are the so-called "Vikings" from the north.";
        

The solution to avoiding this problem is to use the backslash escape character.

The escape backslash (\) converts special characters into the characters of a character unit:

Escape character Result Description
\' ' Single quote
\" " Double quote
\\ \ Backslash

The sequence \" includes double quotes in the series:


Example
String txt = "We are the so-called \"Vikings\" from the north.";
        

The sequence \' includes one quote in the character unit:


Example
String txt = "It\'s alright.";
        

Sequence \\ includes one backslash in the series:


Example
String txt = "The character \\ is called backslash.";
        

Six other escape sequences are allowed in Java:

Code Result
\n New Line
\r Carriage Return
\t Tab
\b Backspace
\f Form Feed


Adding Numbers and Strings

WARNING!

Java uses + operators in both integration and integration

Numbers added. The wires are connected.


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 combination of characters:


Example
String x = "10";
String y = "20";
String z = x + y;   // z will be 1020 (a String)
            

If you add a number and a character unit, the result will be a combination of the character unit:


Example
String x = "10";
int y = 20;
String z = x + y;   // z will be 1020 (a String)