CodingBison

String is one of the widely used data types in any programming language. A string is basically a collection of characters. If you are familiar with "C" programming language, you would know how difficult it is manage string in C. In fact C programming language does not support string data type, a string in C is nothing but a character array. By comparison hand Java provides a much better support for Strings.

String is not a primitive data type in Java; it is actually a Class. When ever a string variable is created using "String <string-name>" we are actually creating an object of the class "String". String class in Java provides wide range of constructors and methods that can be used to manipulate the strings. Let us look at a sample program that helps in understanding the concept of String.



Figure: Sample program demonstrating the usage of data type String.

The above example declare string object in 2 different ways. In the first style we define a string variable and then assign a value to it. In the second style, we define a string variable and also assign a value to it, both in one single step.

Utility Methods

Java String class provides a wide range of constructors and methods that could be used to manipulate String variables. Here is the list of the most commonly used string utility methods.


Table: Commonly used String utility methods.
MethodDescriptionUsage (strVar is a String variable)
length()Returns the length of the stringstringVar.length()
concat()Concatenates 2 stringstringVar.concat(stringVar2)
equals()Compares 2 strings, returns true if equal, else returns falsestringVar.equals(stringVar2)
equalsIgnoreCase()Case insensitive version of equals()stringVar.equalsIgnoreCase(stringVar2)
startsWith()Returns true if stringVar starts with the passed string (stringVar2), else returns falsestringVar.startsWith(stringVar2)
endsWith()Returns true if stringVar ends with the passed string (stringVar2), else returns falsestringVar.endsWith(stringVar2)
compareTo()Returns 0 if the stringVar matches the passed string (stringVar2), else returns a +ve value (if stringVar2 comes after stringVar in dictionary) or a -ve value (if stringVar2 comes before stringVar in dictionary)stringVar.compareTo(stringVar2)
indexOf()Returns the first index of the stringVar2 in stringVar. If not found, it returns -1. Has two versions. One takes a char as input and the other one takes a String as inputstringVar.indexOf(int charIndex) stringVar.indexOf(String stringVar2)
lastIndexOf()Returns the last index of the stringVar2 in stringVar. If not found, it returns -1. Has two versions. One takes a char as input and the other one takes a String as inputstringVar.lastIndexOf(int charIndex) stringVar.lastIndexOf(String stringVar2)
charAt()Returns the character at the specified indexchar c = stringVar.charAt(int charIndex)

Example program

Let us write a sample program, using all the concepts we have seen so far in this section.

 public class StringExample {
     public static void main(String[] args) {
         String stringVar;
         stringVar =  "This is a String";
         String stringVar2 = " variable";
         // Declaration and assignment in the same line.
         // String string_var = "This is a String variable";

         System.out.println("String: " + stringVar + ", Length: " + stringVar.length());
         System.out.println("String: " + stringVar + ", Concatenated String: " + stringVar.concat(stringVar2));
         // Returns -ve number if the stringVar2 is lesser than stringVar, else  return +ve number.
         System.out.println("String: " + stringVar + ", Compare Strings: " + stringVar.compareTo(stringVar2));
         // Return the index of first occurrence of the character.
         System.out.println("String: " + stringVar + ", index of " + stringVar.indexOf('s'));
         System.out.println("String: " + stringVar + ", index of " + stringVar.lastIndexOf('s'));
     }
 }

Compile/Run this program and here is the output:

 String: This is a String, Length: 16
 String: This is a String, Concatenated String: This is a String variable
 String: This is a String, Compare Strings: -34
 String: This is a String, index of 3
 String: This is a String, index of 6




comments powered by Disqus