String handling in Java

String is nothing but a sequence of characters. Various ways in which you can create strings in Java are given below.

  1. char [] city = {‘B’,’r’,’i’,’s’,’b’,’a’,’n’,’e’}
  2. String city = “Brisbane”
  3. String city = new String(city)
  4. String city = new String(“Brisbane”)
  5. StringBuffer city = new StringBuffer(“Brisbane”)
  6. StringBuilder city = new StringBuilder(“Brisbane”)

String in Java

There are 2 types of strings in Java. 

  1. Immutable – Strings created in examples 1 – 4 are immutable strings
  2. Mutable  – StringBuffer and StringBuilder. When you think that strings are going to modified many times, you can use these classes. Difference between StringBuffer and StringBuilder is that StringBuffer is synchronized while StringBuilder is not.

Below Java example illustrates all important methods of String class.

package strings;

/**
 * Created by Sagar on 16-04-2016.
 */
public class strings {

    public static void main(String [] args){
        String city = new String("Brisbane");
        System.out.println("String in focus -> " + city);

        //To get the character at specific index
        System.out.println("Character at index 0 -> " + city.charAt(0));

        //To get the ascii value of a character at specific index
        System.out.println("Ascii value of Character at index 0 -> " + city.codePointAt(0));

        //To compare the strings lexicographically/alphabetically
        System.out.println("Comparing Brisbane with Sydney lexicographically -> " + city.compareTo("Sydney"));
        System.out.println("Comparing Brisbane with Sydney in case insensitive manner -> " + city.compareToIgnoreCase("Sydney"));

        //To concatinate the string
        System.out.println("Concatinating Sydney to Brisbane -> " + city.concat("Sydney"));
        System.out.println("city is still pointing to Brisbane -> " + city);

        //To check if String contains other string
        System.out.println("Brisbane contains bane? -> " + city.contains("bane"));

        //To check if string1's content is equal to string 2
        System.out.println("is Brisbane equal to Sydney? -> " + city.contentEquals("Sydney"));
        System.out.println("is Brisbane equal to Brisbane? -> " + city.contentEquals("Brisbane"));

        //To check if String ends with other string
        System.out.println("Brisbane ends with bane? -> " + city.endsWith("bane"));

        //To check if String starts with other string
        System.out.println("Brisbane starts with bane? -> " + city.startsWith("bane"));

        //To check if String matches with other string in case insensitive manner
        System.out.println("Brisbane is equals to Sydney? -> " + city.equalsIgnoreCase("Sydney"));

        //To check if String variable is empty
        System.out.println("Brisbane is empty? -> " + city.isEmpty());

        //To get the length of the string
        System.out.println("Brisbane length is -> " + city.length());

        //replace - to replace the character in string by other character
        System.out.println("Replacing B by C -> " + city.replace('B','C'));

        //replaceAll - to replace the pattern in string with other string
        System.out.println("Replacing B by C -> " + city.replaceAll("B","C"));
        System.out.println("Replacing [Bb] by C -> " + city.replaceAll("[Bb]","C"));

        //matches - to check if string matches with the pattern as it is
        System.out.println("Brisbane matches with Brisbane? -> " + city.matches("Brisbane"));

        //split - to split the string by delimiter
        System.out.println("Splitting Brisbane by s -> " + city.split("s")[0]);
        System.out.println("Splitting Brisbane by s -> " + city.split("s")[1]);

        //substring - to get the substring from original string
        System.out.println("Substring of Brisbane in the range 4-8 -> " + city.substring(4,8));

        //toCharArray - to convert the string into array
        char [] array = city.toCharArray();
        System.out.println("Brisbane in array -> " + array[0] + array[1] + array[2]);

        //toLowerCase - to convert the string to lower case
        System.out.println("Brisbane in lower case is -> " + city.toLowerCase());

        //toUpperCase - to convert the string to upper case
        System.out.println("Brisbane in upper case is -> " + city.toUpperCase());

        //trim - to remove the white spaces from start and end of the string
        System.out.println("Brisbane after trimming is -> " + city.trim());

    }
}

Here is the output of above Java example
String in focus -> Brisbane
Character at index 0 -> B
Ascii value of Character at index 0 -> 66
Comparing Brisbane with Sydney lexicographically -> -17
Comparing Brisbane with Sydney in case insensitive manner -> -17
Concatinating Sydney to Brisbane -> BrisbaneSydney
city is still pointing to Brisbane -> Brisbane
Brisbane contains bane? -> true
is Brisbane equal to Sydney? -> false
is Brisbane equal to Brisbane? -> true
Brisbane ends with bane? -> true
Brisbane starts with bane? -> false
Brisbane is equals to Sydney? -> false
Brisbane is empty? -> false
Brisbane length is -> 8
Replacing B by C -> Crisbane
Replacing B by C -> Crisbane
Replacing [Bb] by C -> CrisCane
Brisbane matches with Brisbane? -> true
Splitting Brisbane by s -> Bri
Splitting Brisbane by s -> bane
Substring of Brisbane in the range 4-8 -> bane
Brisbane in array -> Bri
Brisbane in lower case is -> brisbane
Brisbane in upper case is -> BRISBANE
Brisbane after trimming is -> Brisbane

You may also like...