String Functions in Java

Strings are objects in Java, representing sequences of letters, numbers, symbols, and whitespace characters . They are immutable, meaning once created, their values cannot be changed and also strings are a fundamental data type used for representing sequences of characters. They're widely utilized for handling textual data in Java applications. Java provides a rich set of built-in functions, or methods, specifically designed for manipulating strings. These functions allow developers to perform various operations on strings efficiently.


  • Commonly Used String Functions:
  • String functions in Java provide various operations to manipulate strings, such as concatenation, slicing, formatting, and searching. Let's explore some common string functions:

    str.toUpperCase(): Converts a string to uppercase.
    str.toLowerCase(): Converts a string to lowercase.
    str.length():returns the length of the string;
    str.trim():Removes any leading and trailing whitespace characters from the string.
    str.split():this function is used to split a string into an array of substrings based on a specified delimiter.
    str.isEmpty(): Returns true if the length of the string is 0, otherwise false;
    str.charAt(): Returns the character at the specified index;
    str.startswith(): Checks if a string starts with a specified prefix.
    str.endswith(): Checks if a string ends with a specified suffix. str1.concat(String str2): Concatenates the str2 string to the end of str1 string.


  • Example Usage of String Functions:
  • Let's see some examples of using string functions in Python:

    # length(): Returns the length of a string

    class PBAINST { public static void main(String args[]) { String a="PBAINSTITUE"; System.out.println(a.length()); } }

    Output:
    11

    # toUpperCase(): Converts a string to uppercase.

    class PBAINST { public static void main(String args[]) { String a="pba institute"; System.out.println(a.toUpperCase()); } }

    Output:
    PBA INSTITUTE

  • Real-life examples demonstrating the use of string functions:
  • Question.1 Write a program to accept a string and check Palindrome or not.
    Example. MOM, MADAM, DAD etc.

    Solution
    import java.util.*; class PBAINST{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); String S=new String(); System.out.println("Enter the String:"); S=sc.nextLine(); String rev=""; String t=S; for(int i=S.length()-1;i>=0;i--){ rev=rev+S.charAt(i); } if(t.equals(rev)){ System.out.println("Palindrome."); } else{ System.out.println("Not Palindrome."); } } }

    OUTPUT: Enter the String:
    DAD
    Palindrome.

    Question.2 Write a program to accept a string and input a char value at the given index number.
    Input: ROMA Enter the Index number=3 Output: M

    Solution
    import java.util.*; class PBAINST{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); String S=new String(); System.out.println("Enter String:"); S=sc.nextLine(); System.out.println(S.charAt(3)); } }

    Output:
    Enter String:
    ROMA
    A

  • Practice Exercise:
  • 1. Write a program to reverse or mirror image the entered string.
    Input: COMPUTER
    Output: RETUPMOC
    2. Write a program to enter a string and change the case of each alphabet of the string.
    Input: Programming Beginner To Advanced Output: pROGRAMMING bEGINNER tO aDVANCED
    3. Write a program to accept a string and find :
    i) number of blank spaces in the string.
    ii) number of words in the string iii) number of characters present in the string.

  • Conclusion:
  • In Java, the 'String' class is fundamental for manipulating text. It provides numerous methods for operations like concatenation, comparison, substring extraction, and more. By utilizing these methods, developers can efficiently handle and manipulate textual data within Java programs. The versatility and robustness of 'String' functions make them indispensable for various applications, ranging from basic string manipulation to complex text processing tasks.