JAVA – string methods – 1

Java is rich with methods to handle strings and strings manipulation. In this post I will detail the most used string methods and usage of each one. As string functions are much and important I will make this post divided to several posts in a series to be easy to follow and to give time to exercise.

length()

This method Returns the length of the sequence of characters represented by this object.

Syntax:

Example:

Result:

substring()

This method has two deferent inputs one integer or two integer parameters as character indexes. The method with only one parameter returns a part of the string starting from the integer index. The method with only two parameters returns a part of the string starting from the first integer index and ends before the second index value.

Syntax:

or

i  the begin index, inclusive.

e  the end index, exclusive.

Example:

Result:

toLowerCase()

This method changes the case condition of the characters to lower case condition. This functions have an optional parameter of the localization attribute used to convert to localized lowercase

Syntax:

Example:

Result:

toUpperCase()

This method changes the case condition of the characters to upper case condition. This functions have an optional parameter of the localization attribute used to convert to localized uppercase.

Syntax:

Example:

Result:

toString()

Returns a string representation of the object. In general, the method returns a string that “textually represents” this object.

Syntax:

Example:

Result:

​trim()

This method returns a copy of the string, with removing leading and trailing whitespaces.

Syntax:

Example:

Result: