String Functions in Java

Introduction

A string is a sequence of characters. Strings are very important in Java. There are various methods of strings in Java. Some of them are as follows.

Creation of string

We can create a string in Java in the following way.

package demo99;

public class Demo99

{

    public static void main(String args[])

    {

        String abc = "one way of creation";

        String bcd = new String("another way of creation");

        System.out.println(abc);

        System.out.println(bcd);

    }

}

Output

creation of string

Copying Values of Array to String

We can copy the values contained by an array to the specific string in the following manner.

package demo99;

public class Demo99

{

    public static void main(String args[])

    {

       char ch[] = {'c','o','m','p','u','t','e','r'};

       String Str = String.copyValueOf(ch);

       System.out.println(Str);

    }

}

Output

copying array values to string

Concatenation of Two Strings

There is a simple concat() method for the concatenation of strings in Java or we can also concat them in the following manner.

package demo99;

public class Demo99

{

    public static void main(String args[])

    {

        String a = "COMPUTER ";

        String b = "SCIENCE";

        String c = a+b;

        System.out.println("from first way    "+c);

        System.out.println("\n");

        c = a.concat(b);

        System.out.println("from second way   "+c);

    }

}

Output

concatenation of strings

Comparing Two Strings

We can compare two strings using the equals() method as follows.

package demo99;

public class Demo99

{

    public static void main(String args[])

    {

        String a = "COMPUTER";

        String b = "COM";

        String c = "COMPUTER";

        if(a.equals(b))

        {

            System.out.println("strings are equal");

        }

        else

        {

            System.out.println("strings are not equal");

        }

        if(a.equals(c))

        {

            System.out.println("strings are equal");

        }

        else

        {

            System.out.println("strings are not equal");

        }

    }

}

 

Output

comparing two strings

Getting Index of a Character or String From Other String

We can determine the index of a specific character or string from the other string using the indexOf() method in the following way. 

package demo99;

public class Demo99

{

    public static void main(String args[])

    {

        String a = "COMPUTER";

        System.out.println("Index of character 'T' is "+a.indexOf('T'));

        System.out.println("Index of string 'PUT' is "+a.indexOf("PUT"));

    }

}

 

Output

finding index

Replacing Characters in the String

We can easily replace a character in the string with another character using the replace() method as follows.

package demo99;

public class Demo99

{

    public static void main(String args[])

    {

        String abc = "COMPUTER";

        System.out.println("Replace 'R' with 'S' : "+abc.replace('R', 'S'));

    }

}

 

Output

replacing character from string

Changing the Case of Characters of the String

We can change the case of the characters of the string by using the toLowerCase() and toUpperCase() methods as follows.

package demo99;

public class Demo99

{

    public static void main(String args[])

    {

        String abc = "COMPUTER";

        System.out.println("To Lower Case: "+abc.toLowerCase());

        System.out.println("To Upper Case: "+abc.toUpperCase());

    }

}

 

Output

changing case of characters in string

Summary

This article explains the various string functions in Java.

Up Next
    Ebook Download
    View all
    Learn
    View all