String Method in Java - Part-1
In the last blog, we learned about String in Java. If you want to learn more about the String then visit Strings in Java. 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. Following are the methods:
getBytes()
getBytes() is used for encoding the string into a sequence of bytes using UTF-8(default charset of the platform) and storing its result in the new byte array.
Syntax
e.g
Output
getChars()
getChars() is used for copying the content of the current string into the character array.
Syntax
sourceBeginIndex - is the index from where the characters should be copied.
sourceEndIndex - is the index until where the last character should be copied from the source string.
destination - destination array where the string will be copied.
destinationBeginIndex - is the index from where the characters from the string should start pushing elements.
e.g
Output
indexOf()
indexOf() is used when we want to know the position of the specified character or sequence of characters(string) that occurs first in the string.
Syntax
used when we want to know the index position of the character value.
It returns the index position of the character value and from which index to start.
returns the index position of the given sequence of characters mentioned in the parameter i.e. string.
returns the index position of the string, and fromIndex is used for the index from where to start.
e.g
Output
isEmpty()
isEmpty() is used for checking if the input string is empty or not, if the string is empty (i.e. if its length is 0) then it returns true, otherwise returns false.
Syntax
e.g
Output
Blank strings can also be handled using isEmpty()
e.g
Output
join()
join() can be used in the string where we want to add a specific delimiter like -, etc. A delimiter will be added to each and every word before starting every word.
Syntax
e.g
Output
null cannot be used as a delimiter, if used then null pointer exception will be thrown. Let's see it through the example:
e.g
Output
But if we attach some elements with the delimiter from which some are string and some are null, then the exception will not be thrown, null will be considered as a normal string. Let's see this with the help of an example:
e.g
Output
lastIndexOf()
lastIndexOf() is used when we want the last index of the character or the string. If the character or string is not found, then it returns -1. Following are the types of lastIndexOf().
Syntax
It returns the position of the last index of the given character.
It returns the position of the last index of the given character and fromIndex is from where to start the count of the string.
It returns the position of the last index of the given string
It returns the position of the last index of the given string and fromIndex is the index from where to start counting the string.
e.g
Output
length()
length() method is used when we want to find the length of the specific string.
Syntax
e.g
Output
replace()
replace() is used when we want to replace the old characters or sequence of characters with the new characters in the string.
Syntax
e.g
Output