Strings
- Generally, strings are a sequence of characters, but in Java strings are objects.
- String is immutable which means it cannot be changed cannot be changed. Whenever we change any string, a new instance is created.
- The String is a class but can be used as a reference data type.
Different ways to create a string in java:
- By using string literal.
- By using the new keyword.
String using string literal
- We use double quotes ("") to create string using string literal.
- JVM verifies if the same string is already present in the string constant pool or not.
- If it is already present, then JVM returns a reference to the pooled instance, otherwise, a new string instance is created.
String using new keyword
- When we create a string using new, a new object is always created in the heap memory.
Different ways to print in Java
We can use the following ways to print in Java:
- System.out.print() -> Simply prints anything as string inside the inverted commas except escape sequences.
- System.out.println() -> Prints a new line at the end.
- System.out.printf()
- System.out.format()
Output :
The int value is 25
The float value is 4.210000
The character is D
The string is Deepak Chandra Sharma
The int value is 25
The float value is 4.210000
The character is D
The string is Deepak Chandra Sharma
String Methods in Java
String methods operate on Java Strings. Some of the commonly used String methods are:
Methods | Description |
---|---|
length() | Returns the length of String. |
toLowerCase() | Converts all the characters of the string to the lower case letters. |
toUpperCase() | Converts all the characters of the string to the upper case letters. |
trim() | Removes beginning and ending spaces of the string. |
substring(start_index) | Returns a substring from start index to the end. |
substring(start_index, end_index) | Returns a substring from the start index to the end index. |
replace("old character", "new character") | It replaces all occurences of the specified char value. |
startsWith("character") | Returns true if the string begins with "character". |
endsWith("character") | Returns true if the string ends with "character". |
charAt(index) | Returns the character at a given index position. |
indexOf("character") | Returns the index of the first occurrence of the specified character in given string. |
lastIndexOf("character") | Returns the last index of the specified character. |
equals("other_string") | Returns true if the string equals to the other_string in inverted commas. |
equalsIgnoreCase("other_string") | Returns true if the string equals to the other_string ignoring the case of characters. |
Output :
The length of string is : 17
The lower case string is : deepak sharma
The upper case string is : DEEPAK SHARMA
The trimmed string is : DEEPAK sharma
The sub-string from sixth position is : PAK sharma
The sub-string from sixth position to thirteenth position is : PAK shar
The new string with some replaced words is : ' DEEPAK sherme
The new string with a group of replaced words is : ' DEEPAK shAarmAa
Is the string starts with ' DEE' ?: true
Is the string ends with 'ma' ?: false
What is the character at position 3 ?: D
What is the index of 'a' ?: 11
What is the index of 'a' counting starting from position 3 ?: 11
What is the last index of 'a' ?: 14
What is the last index of 'a' checking from position 17 : 14
Is the string equals to ' DEEPAK sharma ' ?: true
Is the string equals to ' Deepak Sharma ' ignoring sensitivity of cases ?: true
Comments
Post a Comment