>
The String Class: (Part 1)
- The String class is under Lang package in Java. But as this package is by-default imported in Java, there is not need to write the import statement to use String in your program.
- The char type represents only one character. To represent a string of characters, we can use the data type called String. For example, the following code declares message to be a string with the value "Welcome to Java".
- A String object is immutable: Its content cannot be changed once the string is created.
String message = "Welcome to Java";
Creating Strings:
- A String is combination of one or more characters. There are three methods to create Strings:
- By creating object of String class:
- Syntax:
String object_name = new String(“any_string”); - For example:
String name = new String(“Mr. Harvey Specter”);
- Syntax:
- By creating a String variable or literal:
- Syntax:
String var_name = “Value”; - For example:
String name = “Mr. Mike Ross”;
- Syntax:
- From character array:
- Syntax:
String name = new String(Name_Of_Char_Array); - For example:
char value[] = {‘a’, ‘b’, ‘c’, ‘d’, ‘e’};
String name = new String(value);
- Syntax:
- By creating object of String class:
Simple Methods of String class:

- Example: (Length)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
class test { public static void main(String ar[]) { String str = "Welcome to Java!"; System.out.println(str.length()); // prints 16 (space included) } } - Example: (charAt - Part 1)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
class test { public static void main(String ar[]) { String str = "Welcome to Java!"; System.out.println(str.charAt(3)); // prints c } } - Example: (charAt - Part 2)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
class test { public static void main(String ar[]) { String str = "Welcome to Java!"; for(int i=0;i<str.length();i++) { System.out.println(str.charAt(i)); // prints all the characters in the string } } } - Example: (concat)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
class test { public static void main(String ar[]) { String str1 = "Welcome to Java "; String str2 = "world!"; String str3 = str1.concat(str2); // prints Welcome to Java world! System.out.println(str3); String str4 = str2.concat(str1); System.out.println(str4); // world!Welcome to Java } } // spot the diff between str3 and str4 - Example: (toUpperCase)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
class test { public static void main(String ar[]) { String str1 = "Welcome to Java "; String str2 = str1.toUpperCase(); System.out.println("Original: "+ str1); // prints Original: Welcome to Java System.out.println("New: "+ str2);// prints New: WELCOME TO JAVA } } - Example: (toLowerCase) (Opposite to toUpperCase())
- Example: (trim)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
class test { public static void main(String ar[]) { String str1 = " Welcome to Java "; String str2 = str1.trim(); System.out.println("Original:"+ str1);// prints Original: Welcome to Java System.out.println("After trim:"+ str2);// prints After trim:Welcome to Java } }
Comments
Post a Comment