Defining Strings

Learn how to define strings in Java

Defining Java Strings

The Java platform has a class known as the String Class that allows for the creation and manipulation of strings.

We can define a simple string like this

String myFirstString = "This is a string.";

The code snippet above calls the String Class, with the user defined name 'myFirstString' and finally an assignment value = "This is a string."

Like in other programming languages, there is a way to concatenate strings (Concatenation is interconnecting multiple string). The easiest way being:

String concatenate = "This" + "is a concatenated" + "String";

Some commonly used String Methods are listed below:


This returns the number of characters within the String.

String myFirstString = "This is a string.";

int len = myFirstString.length();

System.out.println("The string has " + len + " characters in it.");

This would produce the following: The string has 17 characters in it.


This method will compare the string with a specified object. The result that occurs is only true if the strings are an exact match.

String compareOne = new String("match!");
String compareTwo = new String("match");
String compareThree = new String("match!");

boolean valueRet;

valueRet = compareOne.equals(compareTwo)
System.out.println("Value returned is " + valueRet);
valueRet = compareOne.equals(compareThree)
System.out.println("Value returned is " + valueRet);

The program should return these values:

You may think, wait they have the exact same word in it. However the second string has a missing '!' making both the first string and the second string unequal.

Value returned is false

Value returned is true

Both the first string and the third string have exactly the same characters so the program will recognise them as being equal.


This method has four different variants, the parameters then return the value of the index from the string.

Remember that indexing starts at 0 rather than 1.

If a parameter is not found within a string then the index value of -1 is returned.

String stringOne = new String(", the place to be!");
String stringTwo = new String("place");
String stringThree = new String("sugar");

// Search for the first instance of the letter 'l' in stringOne
System.out.print("Index number of: ");
System.out.println(stringOne.indexOf( 'l' ));

// Search for the first instance of the letter 'n' in stringOne after the 6th character
System.out.print("Index number of: ");
System.out.println(stringOne.indexOf( 'n', 5 ));

// Search for the first instance of stringTwo in StringOne
System.out.print("Index number of: ");

// Search for the first instance of stringTwo in StringOne after the 24th character
System.out.print("Index number of: ");
System.out.println(stringOne.indexOf(stringTwo, 23 ));

// Search for the first instance of stringThree in StringOne
System.out.print("Index number of: ");

Index number of: 0
Index number of: 10
Index number of: 22
Index number of: -1
Index number of: -1


There are two variants of this method. The substring method returns a new string that is a substring of the original string.

String stringOne = new String("This is a new string that we will test.");

// This will make a new string from the 9th character
System.out.print("New substring is: " );
System.out.println(stringOne.substring(8) );

// This will make a new string between the 13th character and 22nd (it ignores the 23rd character as indexing starts from 0)
System.out.print("New substring is: " );
System.out.println(stringOne.substring(12, 22) );

The result will be as follows:

New substring is: a new string that we will test.
New substring is: w string t


The charAt method will return the character located at the specified index number.

String newString = "Yet another test string... *Sigh*";
char result = newString.charAt(12);

The 13th character is returned in this instance: t

For more detail of the String Class click here;


blog comments powered by Disqus