String Class Methods Program in Java

Problem Description

Write a Java Program to Create Strings and How to Use Some Important Methods of String Class.

What is String Class?

The String class in Java is a built-in class that represents a sequence of characters. It is immutable and provides a range of methods for examining individual characters of the sequence, comparing strings, searching strings, extracting substrings, and creating a copy of a string with all characters translated to uppercase or to lowercase.

String Class Methods in Java
The String class in Java offers various methods for working with strings. Methods that are regularly utilized include:

  • length(): It returns the string length.
  • charAt(int index): Gives the character at a particular index of the string.
  • concat(String str): It adds the specified string to the end of the existing string.
  • equals(Object obj): It compares the string with the given object to see if they have the same value.
  • equalsIgnoreCase(String anotherString): It checks if two strings have the same value, but it doesn’t care if the letters are in uppercase or lowercase.
  • indexOf(int ch): This method returns the position of the first occurrence of a specific character in the string.
  • substring(int beginIndex, int endIndex): This method returns a part of the original string as a new string. It takes two parameters – the starting index and the ending index – and creates a new string that includes all the characters between those two indexes.
  • toUpperCase(): It creates a new string that is the same as the original string, but with all the characters changed to their uppercase equivalent.
  • toLowerCase(): This method creates a new string that has all the characters of the original string in lowercase letters.
  • replace(char oldChar, char newChar): It replaces every instance of a certain character in the original string with a new character and creates a new string with the modified characters.
Program/Source Code

Here is the source code of the Java Program to Create Strings and How to Use Some Important Methods of String Class. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /*
  2.  * String Class Methods Program in Java
  3.  */
  4.  
  5. public class Important_Methods
  6. {
  7.     public static void main(String... a)
  8.     {
  9.         String str1 = "Hello";
  10.         String str2 = "from";
  11.         String str3 = "JAVA";
  12.         int len = str1.length();
  13.         System.out.println("The length of the String str1 using length() is "+len);
  14.         String str4 = str1.concat(str2).concat(str3);
  15.         System.out.println("The string after concatenation using concat() is " +str4);
  16.         char c = str4.charAt(5);
  17.         System.out.println("The character atindex 5 using charAt() is  " +c);
  18.         String str5 = "AbCdEfGhIjKlMnOpQrStUvWxYz";
  19.         String str6[]= str5.split("M");
  20.         String part1 = str6[0];
  21.         String part2 = str6[1];
  22.         System.out.println("The string after spliting using split() will be = "+part1+" "+part2);
  23.    }
  24. }
Program Explanation

1. The program creates a public class with a main method. Inside the main method, the program creates three String variables: “str1” with the value “Hello”, “str2” with the value “from”, and “str3” with the value “JAVA”.
2. The program uses the “length()” method on the “str1” String variable to get its length and assigns the result to an integer variable named “len” and prints the length of “str1”.
3. The program combines the values of “str1“, “str2“, and “str3” using the “concat()” method and assigns the result to a new variable named “str4”. Then, the program prints the resulting String “str4“.
5. The program uses the “charAt()” method on “str4” to get the character at index 5 and assigns it to a character variable named “c” and prints the “c” value.
6. A new String variable “str5” is created with the value “AbCdEfGhIjKlMnOpQrStUvWxYz“, and the program uses the “split()” method on “str5” to split it into two Strings at the letter “M“.
7. The resulting Strings are assigned to an array of Strings named “str6“. The program assigns the first String in the “str6” array to a String variable named “part1” and the second String to a String variable named “part2“, which are printed with a space in between them.

advertisement
advertisement
Program Output
$ javac Important_Methods.java
$ java Important_Methods
 
The length of the String str1 using length() is 5
The string after concatenation using concat() is HellofromJAVA
The character atindex 5 using charAt() is  f
The string after spliting using split() will be = AbCdEfGhIjKl and nOpQrStUvWxYz

To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”.

Note: Join free Sanfoundry classes at Telegram or Youtube

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.