String Functions Java Program

String Functions Java Program. In this Topic we will understand the basic String method defined in the String class, Which is one of the important classes in Java therefore it is necessary to understand their practical implementation in the form of a Java Program.

Use of String Class:

It is the most commonly used class, Which can be used to store String constants. A constant can be anything like- character, special symbols, digits, alphanumeric, etc but must be enclosed in double-quotes.

We can concatenate two strings, change character case to uppercase or lowercase, and whatnot, Because of all this functionality String class has very special importance in Java.

String Basic Methods:

Sr.NoString MethodSyntaxExplanation
1.length()int length()Returns the length of the Given String.
2.equals()boolean equals(Object anObject)Compares String to Specified object.
return true if two Strings are equal
else return false.
3.charAt()charAt(int index)Compare two Strings
for example two Strings s1,s2
if s1<s1- return negative
s1>s2-return positive
s1=s2-return 0
4.compareTo()int compareTo(String, to anotherString)Returns the index of the first occurrence of the specified character.
5.indexOf()int indexOf(int ch)Returns the index of first occurrence of the specified character.
6.substring()String substring(int beginIndex)It Returns a new substring specified by the index from an existing string.
7.substring()substring(int beginIndex,int endIndex)Returns a new substring specified by the begin index and end index from the existing string.
begin index is the start point and the end index is the endpoint for a substring.
8.replace()replace(char old,char newchar)It Returns a new string after replacing an old character with a new character.
9.concat()String1 concat(string2)Returns Concatenated String.

String Functions Java Program

ProgramJava Program to Perform basic String Operation

/*
Java Program to Perform Basic String Operations */
import java.util.Scanner;
import java.util.Arrays;
public class StringMethods {
    public static void main(String args[]) {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter String-1");
        String s1=sc.nextLine();
        System.out.println("Enter String-2");
        String s2=sc.nextLine();
        System.out.println("Length of String-1 is:"+s1.length());
        System.out.println("Length of String-2 is:"+s2.length());
        System.out.println("String-1 in LowerCase-"+s1.toLowerCase());
        System.out.println("String-2 in UpperCase-"+s2.toUpperCase());
        System.out.println("Check s1 equals s2-"+s1.equals(s2));
        System.out.println("Comapre s1 & s2:"+s1.compareTo(s2));
        System.out.println("Chatracter of String at postion 0 is:"+s1.charAt(0));
        String s3=s1.concat(s2);
        System.out.println("Concatened String is:-"+s3);
        System.out.println("Substring is:"+s3.substring(0,11));
        System.out.println("After Replacing o with a-"+ s3.replace('o','a'));
        System.out.println("Index of Character N is:"+s3.indexOf('n'));
      /*  s1= s1.toLowerCase();
        s2=s2.toLowerCase();
        String s3=s1.concat(s2);
        s1.equals(s2);
        s1.compareTo(s2);*/

}}
Output:
Enter String-1
Noteshacker
Enter String-2
Website
Length of String-1 is:11
Length of String-2 is:7
String-1 in LowerCase-noteshacker
String-2 in UpperCase-WEBSITE
Check s1 equals s2-false
Comapre s1 & s2:-9
Chatracter of String at postion 0 is:N
Concatened String is:-NoteshackerWebsite
Substring is:Noteshacker
After Replacing o with a-NateshackerWebsite
Index of Character N is:-1

Related Topics

Leave a Comment