String Method in Java - Part-2
In the last blog, we learned about String in Java. If you want to learn more about the String then visit String Methods in Java Part 1. In the Strings, in Java blog, we have learned some methods of String. In this blog, we will continue with the remaining methods of String. The following are the methods:
replaceAll()
replaceAll() method gives us the string replacing all the sequence of characters matching regular expressions and replacement string.
Syntax
e.g
Output
split()
split() breaks(splits) the string into multiple strings by providing the delimiter which separates the string.
Syntax
e.g
Output
Above example is without using any limit just displaying every word of the string in next line.
Now we will see how we can split the string by giving the limit to it
e.g
Output
startsWith()
startsWith() method is used when we want to check if the string starts with the mentioned prefix. If the string contains the given prefix, then it returns true, otherwise returns false.
Syntax
e.g
Output
Now we will see the example of startsWith() having offset, offset is nothing but the index from where we want to start matching the prefix in the string.
e.g
Output
In the above example for Shortkicks offset is 5 and there is no offset mentioned for Java Shortkicks, so the offset considered will be 0 by default.
substring()
substring() is used to extract the substring from the large string.
Syntax
e.g
Output
toLowerCase()
toLowerCase() returns all the characters of the string in lowercase letters.
Syntax
e.g
Output
toUpperCase()
toUpperCase() returns all the characters of the string in uppercase letters.
Syntax
e.g
Output
trim()
trim() is used for eliminating the beginning and ending spaces in the string.
Syntax
e.g
Output
valueOf()
valueOf() is used for converting the different types of values like int, float, long, boolean, etc into the string. Using this method we can convert int to string, float to string, long to string, boolean to string, etc.
Syntax
e.g
Output
Mostly asked Questions
String abc = "abc";
System.out.println(abc=="abc");
Creates space in stack to store the reference abc. It creates space in the String literal pool to store the constant i.e. abc. Reference abc will point to the constant(abc) in the pool. The address of the reference abc is the same as "abc" used in the print statement. So it will print true here as both have the same address i.e. abc.
String stringObj = new("ABC");
System.out.println(stringObj =="ABC");
Creates a space in the stack to store the reference i.e. stringObj. Creates a space in the heap to store the constant("abc") in the heap. Reference stringObj points to the newly created object("abc") in the heap. The address of the reference object(stringObj) is the address in the heap and the constant("abc") address is in the string constant pool, so the result will be false.
String abc = "ABC";
String strObj = new("ABC");
String strObj1 = new("ABC");
System.out.println(abc == strObj);
System.out.println(strObj == strObj1);
In the first statement space will be created in the stack to store the reference and for string, the string constant space will be created in string constant pool. In the second statement, a space will be created in the stack for the reference (strObj) and a separate space will be created for the string (abc) in the heap. In the third statement, the same process will be followed as in the second statement. The next statement will return the result false because abc address is in the String pool and strObj address is in the heap. The same result will be returned for the last statement i.e. false because the same will be applied here. strObj address is the address in the heap but it is located at a different location, and strObj1 will also be stored in the heap memory, but it will have a different location as it is created using the "new" keyword.