Java Strings


In this tutorial, we will discuss what methods can be applied to Strings using the String class. You should know how to declare a String in Java by now. This tutorial is easy and very helpful because many operations can be done on strings. Let's begin by showing you a table of some of the string methods that you can use in Java.

Java String Methods

Method Return Type Description
charAt(int index) char returns the character at position index in the string
endsWith(String suffix) boolean checks if the string ends with suffix
indexOf(String str) int returns the index of the first occurrence of str in the string or -1 if str is not found
lastIndexOf(String str) int returns the index of the last occurrence of str in the string or -1 if str is not found
length() int returns the length of the string
replaceAll(String regex, String replacement) String repleaces every string regex with the string replacement
startsWith(String suffix) boolean checks if the string begins with suffix
subString(int beginIndex) String returns a new string that is a substring of this string from begin index untill the end
subString(int beginIndex, int endIndex) String returns a new string that is a substring of this string from beginIndex untill endIndex
toLowerCase() String converts any uppercase letter to lowercase
toUpperCase() String converts any lowercase letter to uppercase

Let's take a look now at a coding example to check how some of these methods work.

public class StringTest
{
   public static void main(String args[])
   {
      String str = "Java Judge";

      char fourth = str.charAt(4);
      System.out.println("charAt(4) = "+fourth+"\n");

      System.out.println(str.endsWith("ge")+"\n");

      int indexOfU = str.indexOf("u");
      System.out.println("index of u is: "+indexOfU+"\n");

      int lastJ = str.lastIndexOf("J");
      System.out.println("last index of J is: "+lastJ+"\n");

      System.out.println("Length of str is: "+str.length()+"\n");
   }
}

Output:


charAt(4) = a

true

index of u is: 7

last index of J is: 6

Length of str is: 10

Comparing Strings

Comparing strings is a little bit more complicated than comparing primitive type variables. We usually use the equal and less than or greater than symbols but with strings we have to use string methods.

To compare if two strings are equal we use the method .equals(string to compare). Take a look at the example below to see how we compare strings in Java.

public class CompareStrings
{
   public static void main(String args[])
   {
      String str = "JTUTS");

      if(str.equals("JTUTS"))
         System.out.println("Equal Strings");
      else
         System.out.println("Not Equal");

      if(str.equals("Hello"))
         System.out.println("Equal Strings");
      else
         System.out.println("Not Equal");

      if(!str.equals("Juice"))
         System.out.println("Not Equal");
      else
         System.out.println("Equal Strings");

      if(str.equals("jtuts"))
         System.out.println("Equal Strings");
      else
         System.out.println("Not Equal");
   }
}

Output:

Equal Strings
Not Equal
Not Equal
Not Equal

Notice that we used ! in the third if statement. This checks if the result is false. Moreover, look at the final if statement, we compared to "jtuts" but the result was false. This is because Java is case sensitive and if the strings are not in the same case, it will consider them different.

Now let's move on to checking which string is greater or less than the other. Comparing strings is done alphabetically, which means characters are checked to see which character codecedes the other. To compare strings, we use the .compareTo(another string) method. The compareTo method returns 0 if the strings are equal, -1 if the string is less than the other string, and 1 if it is greater. Let's check this in code.


public class CompareStrings
{
   public static void main(String args[])
   {
      String str = "Linux";
      String str2 = "Windows";

      int compare = str.compareTo(str2);

      if(compare > 0)
         System.out.println("Linux is greater");

      else if(compare < 0)
         System.out.println("Windows is greater");

      else System.out.println("Both are equal");
   }
}

Output:

Linux is greater

Congratulations

You now know how to use some string methods and how to compare strings in Java. Move on to the next tutorials but first check the following important notes.

Important Notes

Ignoring Case When Comparing

We said that if we want to check if two strings are equal, Java differentiates between upper case and lower case letters. Therefore, to get around this we use the methods compareToIgnoreCase and equalsIgnoreCasr.