Java Strings: Operations and Methods

07 Feb 2023
Beginner
313 Views

Java Strings: Operations and Methods

Introduction

Java strings are specific types of objects of java.lang, which is a class that represents a sequence of characters. For creating and manipulating Strings, the Java platform needs a String class, these are the string functions in java. Strings are mainly used in Java to make the memory of Java more efficient.

What are Strings in Java?

In Java, String does not identify as a Datatype. It identifies as a class. Any programmer can create a String by instantiating the String class that is situated in the package of java.lang. It assists to generate some Methods and construction for creating, manipulating, and searching a String. The objects of Strings are immutable that's why they can not be overwritten. If there are any alterations needed in string operations in java, it creates a new object.

How to Create a String Object?

There are a few steps to create a String Object in Java, those are:

Example

class String_Creation_Demo
{
    public static void main(String[] args)
    {
        String ob1 = new String(); //creates an empty string

        //new keyword helps to create an object of class String
        System.out.println("Empty String: " +ob1);

        char arr[] = {'j','k','a','q','e' };
        String ob2 = new String(arr); //String from an array

        System.out.println("Contents of Array String: " +ob2);

        String ob3 = new String(arr,1,2); //String from subsequence of an array
        System.out.println("Contents of subsequence of Array String: " +ob3);

        String ob4 = new String(ob3); //String from another string object
        System.out.println("Contents of Array String ob4: " +ob4);
    }
}

Output

Empty String: 
Contents of Array String: jkaqe
Contents of subsequent of Array String: ka
Contents of Array String ob4: ka

Java String Class Methods

Java string methods are important for manipulating the content of a string so that it changes accordingly. There are various methods, which will be discussed shortly in this article

String Manipulation

String manipulation mainly works for changing the case, fetching a character from a string, and trimming the content. There are other various important methods in string manipulation such as:

Example

class String_Manipulation_Demo
{
    public static void main(String[] args)
    {
        String ob1 = "Scholar-Hat"; //creates a string object

        //string length
        System.out.println("Length of the String: " +ob1);

        char arr[] = {'j','k','a','q','e' };
        String ob2 = new String(arr); //String from an array

        //string concatenation
        System.out.println("Concatenate String and String Array: " +ob1.concat(ob2));

        //to upper case
        System.out.println("Contents of String in uppercase: " +ob1.toUpperCase());

        //to lower case
        System.out.println("Contents of String in lowercase: " +ob1.toLowerCase());

        //split function
        for(String res: ob1.split("-",2))
        System.out.println("Splitting the String: " +res);

        //contains function
        System.out.println("Contains() function in String: " +(ob1.contains("Scholar")));

        //Replace function
        System.out.println("Replace function in String: " +(ob1.replace('o','a')));

        //ReplaceAll function
        System.out.println("ReplaceAll function in String: " +(ob1.replaceAll("lar","o")));

        //substring function
        System.out.println("Substring in String: " +(ob1.substring(3,6)));

        //trim function
        String str = " Scholar-Hat! ";

        System.out.println("Without Trim function in String: " +str);
        System.out.println("Trim function in String: " +(str.trim())); 
    }
}

Output

Length of the String: Scholar-Hat
Concatenate String and String Array: Scholar-Hatjkaqe
Contents of the String in uppercase: SCHOLAR-HAT 
Contents of the String in lowercase: scholar-hat 
Splitting the String: Scholar
Splitting the String: Hat
Contains() function in String: true
Replace function in String: Schalar-Hat
ReplaceAll function in String: Schoo-Hat
Substring in String: ola
Without Trim function in String: Scholar Hat!
Trim function in String: Scholar Hat!

String Comparison

There are some important methods that are used to make the string comparison, such as:

Example

class String_Comparison_Demo
{
    public static void main(String[] args) 
    {
        String ob1 = "Scholar-Hat"; //creates a string object
        String ob2 = "scholar-hat";

        //equals function
        System.out.println("Using equals function: " +(ob1.equals(ob2)));

        System.out.println("Using equalsIgnoreCase function: " +(ob1.equalsIgnoreCase(ob2)));

        //compareTo function
        System.out.println("Using compareTo function: " +(ob1.compareTo(ob2))); 
        System.out.println("Using compareToIgnoreCase function: " +(ob1.compareToIgnoreCase(ob2)));

        //startsWith function
        System.out.println("Using startsWith function: " +(ob1.startsWith("Sc"))); 

        //endsWith function
        System.out.println("Using endsWith function: " +(ob1.endsWith("at"))); 
    }
}

Output

Using equals functions: false
Using equalsIgnoreCase functions: true
Using compareTo functions: 4
Using compareToIgnoreCase functions: 0
Using startsWith functions: true 
Using endsWith functions: true

Searching in a String

There are some specific methods to search characters in a String Object, those are:

Example

class String_Searching_Demo
{
    public static void main(String[] args)
    {
        String ob1 = "Scholar-Hat"; //creates a string object
        System.out.println("Using indexOf function: " +ob1.indexOf('a'));
        System.out.println("Using lastIndexOf function: " +ob1.lastIndexOf('a'));
    }
}

Output

Using indexOf function: 5
Using lastIndexOf function: 9
Summary

In this article, we covered the Java strings methods, string functions in java and string operations in java. As you can see, there are a variety of operations and methods that can be performed on Java Strings. This is just a small sampling of what you can do with strings in Java. With so many options available, you should have no problem finding the right tool for the job. Do some experimenting and see which methods work best for your purposes. 

Accept cookies & close this