Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Java String Explained With Examples

1. Creating Java String

The first thing one should know is that Java Strings are not a primitive data type. They are objects in Java. The String objects once constructed can not be changed. Strings like other programming languages stores texts. In this article we will see basic String Examples.

There are many ways to create a Java String. The below Example shows three ways of creating a String and printing it in the console output window:


Line 6:
Here, we create a string and got reference to it in the variable Str1.

Line 9-10:
Here, we construct the Java String Str2 as an empty string and the string literal and then assign “Second String” string literal to it.

Line 12-15:
In this lines of code we create the array of Characters and store the reference in a variable Chr1. Then, we use this array to create a third Java String object and store its reference in str2.

The output of this First Example is below:

Java String Example 1 - Output
Java String Example 1 – Output

2. Java String Length

The length() method of the String object will return the length of the string. Have a look at the below Example:

This will print “Length:12” in the console output window.

3. Concatenating Java Strings

3.1 Using concat() method

We can concatenate a String in Java using the + operator or using concat() method of the String Object. We will see an example for both. Have a look at the example below:

Note, in the above code we created a string called “Java” and have reference S1. Then we created one more string S2 by appending the “String” with S1 by calling the concat() method. The same way string S3 is created. The string S3 when printed gives – Java String Example as output.

Note: Concatenating string means joining them together

3.2 Using ‘+’ operator

Java does not allow changing the string object content after its construction. Hence, we created three strings and printed S3. The more easy we to join string is shown in the below example:

Here, the + operator is used to join the strings S1 and S2 with the string literal “Example”.

4. Getting String of Characters

4.1 Getting single character from a Java String

The string is a merger of characters packed together. The charAt() method will help to get a specific character from the string. Below is the example:

In the above code, at line 7 the character position at 2 is retrieved and stored in c1. The c1 will have the letter “a” and the same way c2 will have “r”. The below picture shows the output of this example:

Java String charAt() Example - Output
Java String charAt() Example – Output

4.2 Getting Range of Characters From A Java String

The getChars() method of java string is useful to grab range of characters from the string. Now have a look at the below example:


Line 6:
A String S1 is created.

Line 7:
We create a New character array which can hold 7 elements.

Line 8:
The getChars() method of String is called to fill the chrarr array. The first two parameters tell position start and end in the Source String. Third parameter is the destination array which we want to fill. In our case, it is chrarr. The fourth parameter says start location in the destination array.

The program will output: example

5. Getting Sub-String of a Given Java String

The substring() method is used to get subset of the characters from the string. The example for this is  below:


In this example, at line 9, we are making a call to the substring() function. We are passing two parameters which denote the character position of the string. The first parameter is starting location of the sub-string and the second parameter specifies the stop location. With these two parameters, the retrieved sub-string is stored in String S2.

At Line 12, we just pass the start location of the string and sub-string function knows that it should stop at end-of the string. Output of this example is below:

Java substring() Example - output
Java substring() Example – output

6. Searching for String

There are two methods which we can use in java to search a string. The method indexOf() is useful to find the first occurrence of a string. The method lastIndexOf() will find the last occurrence of the string. Both these methods will give position based index which tells found location of the java string. Now, look at the example below:

Below is the output of the Example:

First occurrence of Draw: 3
Last occurrence of Draw: 30

Categories: Java

Tags: , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.