>
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)
- Example: (charAt - Part 1)
- Example: (charAt - Part 2)
- Example: (concat)
- Example: (toUpperCase)
- Example: (toLowerCase) (Opposite to toUpperCase())
- Example: (trim)
Comments
Post a Comment