String

Strings, which are widely used in Java, are a sequence of characters. In Java, strings are objects.

The Java platform provides the java.lang.String class to create and manipulate strings. In this chapter we will look at some commonly used functions to manipulate strings.

Creating Strings

The most direct way to create a string is to write:

Get The Length of a String

length() method returns the length of a string.

Run The Application

Right click to the StringLenthDemo class; select Run As -> Java Application.

Output

Concatenating Strings

The String class includes a method for concatenating two strings. Strings are more commonly concatenated with the + operator.

Run The Application

Right click to the StringConcatDemo class; select Run As -> Java Application.

Output

Creating Format Strings

We can use String.format() or System.out.printf() to format a string.

Run The Application

Right click to the StringFormatDemo class; select Run As -> Java Application.

Output

For Complete String Reference, we will provide in another tutorial.