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.
public class Important_Methods
public static void main(String... a)
String str1 = "Hello";
String str2 = "from";
String str3 = "JAVA";
int len = str1.length();
System.out.println("The length of the String str1 using length() is "+len);
String str4 = str1.concat(str2).concat(str3);
System.out.println("The string after concatenation using concat() is " +str4);
char c = str4.charAt(5);
System.out.println("The character atindex 5 using charAt() is " +c);
String str5 = "AbCdEfGhIjKlMnOpQrStUvWxYz";
String str6= str5.split("M");
String part1 = str6;
String part2 = str6;
System.out.println("The string after spliting using split() will be = "+part1+" "+part2);
$ 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.