Java String Class: Useful Details

Google+
Google+
http://programmingloops.com/java-string-class-useful-details/

Unlike some other programming languages, there is no primitive data types in Java related to string. String is a class belonging to Java.lang package, provides special support for strings, and automatically imports in every program.

A small program example using the String class:
class StringProgram
{
    public static void main(String args[])
    {
        String str = “Hi, there”;
        System.out.println(“The string value is “+str);
    }
}

Initializing String
There are many ways to initialize a string, one is simply initializing the value as mentioned in the above example which is the most prefer way, and the others are through the constructor, here are the examples.
String str = “hi, there”;
char[] chararray = {‘H’,’i’,’ ‘,’t’,’h’,’e’,’r’,’e’};
String str = new String (chararray);          // via constructor
String str1 = new String (“Hi, there”);       // via constructor

String is widely use in programming, and have many constructors and methods.

String class constructors

Default constructor: Whenever there is a need to use String class properties, you create the object, and the default constructor gets initialize. For an example.
String s = new String()

Parameterized constructor: There are a few parameterized constructors in the String class. In these constructors, you can pass the value of character array, byte array and string, like:
String(String)
String(char[])
String(byte[])

Parameterized constructors performing operations on string: There are some parameterized constructors that perform operations on string when you create the String class object, such as:
String(char[],int,int)
String(int[],int,int)
String(byte[],int,int,String)

String class methods
charAt(int): This method is use for searching a character at a specific location in a string.

length(): In many programs, there is a requirement to find the length of a string. This method do exactly the same as it returns an integer value that is the length of the string.

concat(String): The concatenation of two strings is done frequently, concat method is use for doing this.

equals(Object): This is a boolean method, which compares a string with the other string. If both the strings are equal, it returns a true value.

A small program example using some String class constructors and methods:
import java.io.*;
class StringProgram2
{
     public static void main(String args[]) throws IOException
     {
         BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
         char[] chararray = {‘H’,’i’,’,’,’ ‘,’t’,’h’,’e’,’r’,’e’};
         String str = new String(chararray);
         String str1 = br.readLine();
         boolean b = str.equals(str1);
         System.out.println(“The entered value of string is equal to existing value : “+b);
         String str2 = “, How are you?”;
         String str3 = str.concat(str2);
         System.out.println(“Concatenating the two strings value give us : “+str3);
     }
}