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

«
»
This is a Java Program to Create Strings and How to Use Some Important Methods of String Class.

The class String includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase. There are some examples to show use of some methods of String.

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.

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

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

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement

Here’s the list of Best Reference Books in Java Programming, Data Structures and Algorithms.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn