Articles

What is a String and Explain Java String Class Methods ?

Share

What is String ?

A string is a sequence of characters and considered as a constant because it cannot be changed once it’s created, making it immutable. “xyz” , “ijk” are the examples. In Java, all strings are an instance of String class. However, string buffers support mutable strings( strings that can be changed).

For example

String str = “hi”;
System.out.println(str +“buddy”);

What are the important Java String Class Methods?

String Class provides a set of methods for searching, comparing, extracting and concatenating strings.These are some method that provides Java String Class :

1) Char charAt(int position)

It returns character from the mentioned position.

Program:

public class StringProgram
{
  public static void main (String[]args)
  {
    String str = "String";
    char ch = str.charAt (4);
      System.out.println (ch + " at given position in " + str);
  }
}

Output:

2) Int compareTo(String str)

It tells which one of them comes first alphabetically.It returns an integer value,

  • It returns 0, when two strings are identical
  • It returns negative or lesser than 0, when a string which triggered the compareTo() method would alphabetically come first.
  • It returns positive or greater than 0, when a string which is in the argument would come first alphabetically.

Program

public class StringProgram
{
  public static void main (String[]args)
  {
    String str = "apple";
    String str1 = "age";
    if (str.compareTo (str1) == 0)
      System.out.println ("identical");
    else if (str.compareTo (str1) < 0)
        System.out.println (str1 + " comes after " + str);
    else
        System.out.println (str + " comes after " + str1);
  }
}

Output:

3) String concat(String str)

It appends one string to another. It returns a newly created string.

Program:

public class StringProgram
{
  public static void main (String[]args)
  {
    String str = "Good ";
    String str1 = "Morning ";
    String str2 = str.concat (str1);
      System.out.println (" New string = " + str2);
  }
}

Output:

4) Boolean IsEmpty()

It returns true if length() is 0. In other words, It checks whether a string consists of a character or not.

Program:

public class StringProgram
{
  public static void main (String[]args)
  {
    String str = "";
    if (str.isEmpty ())
        System.out.println ("It is empty");
    else
        System.out.println ("It has characters");
  }
}
Note: Whitespaces are also characters.

Output:

5) String replace ( char old , new char)

It replaces all the old characters with a new one.  It returns a new altered string.

Program:

public class StringProgram
{
  public static void main (String[]args)
  {
    String str = "The string methods";
    String str1 = str.replace ('t', 'M');
      System.out.println ("Old String = " + str + "\n New String = " + str1);
  }
}

Output:

Note:  ‘T’ and ‘t’ are not the same

6) Char[] toCharArray()

It breaks down a particular string into an array of characters.

Program:

public class StringProgram
{
  public static void main (String[]args)
  {
    String str = "Example";
    char arr[];
      arr = str.toCharArray ();
      System.out.println (" String = " + str);
    for (int i = 0; i < arr.length; i++)
      {
  System.out.println (arr[i]);
      }
  }
}

Output:

7) String toLowerCase() and toUpperCase()

toLowerCase() turns all the characters in strings to lower case

toUpperCase() converts a string to uppercase.

Program:

public class StringProgram
{
  public static void main (String[]args)
  {
    String str = "Example";
      System.out.println (" Uppercase = " + str.toLowerCase ());
      System.out.println (" Lowercase = " + str.toUpperCase ());
  }
}

Output:

8) String ToString()

It represents a string as an object. Each Object that we create is a part of Object class which consists of toString() method and when we try to print the object using System.out.Println method, the compiler uses this predefined toString method and produces hashcode which is not desirable. To make it correct, we have to override this toString method.

Program:

class ABC
{
  String name;
  int no;
  public String toString ()
  {
    return "Name = " + name + " \n number = " + no;
  }
}
public class Stringprogram
{
  public static void main (String[]args)
  {
    ABC obj = new ABC ();
      obj.name = "Devine";
      obj.no = 11;
      System.out.println (obj);
  }
}

Output:

9) String substring(int strartingpoint) &  String substring(int begin,int end)

It extracts a substring from this string which  takes a single argument that is the starting point.

It extracts a substring from the string as per specified range which takes two arguments that are starting points and end points.

Program:

public class StringProgram
{
  public static void main (String[]args)
  {
    String str = "This is a string";
    String substr = str.substring (10);
    String substr1 = str.substring (8, 13);
      System.out.println (" Using substring method with single arguement = " +
        substr);
      System.out.println (" Using substring method with two arguements = " +
        substr1);
  }
}

Output:

10. Trim() It removes all the whitespaces that come before and after the string.

This post was last modified on January 2, 2021

Sandeep Verma

Published by
Sandeep Verma
Tags: java java string class java string class important method java string class methods string methods what is string ?