String Methods

This tutorial will guide on how to manipulate strings in Java. You will learn the following String Methods:
toUpperCase
toLowerCase
compareTo
indexOf
substring
equals
charAt
trim
valueOf

toUpperCase method, as the name implies, it will convert the string characters to UpperCase. e.g. "name" to "NAME".

 toLowerCase method is the opposite of the toUpperCase method. e.g. "NAME" to "name".

compareTo method is used to compare strings. You will see code examples later.

indexOf method is used to return the index of a specific character on a String. e.g. "name", what is the index of the character 'm'?

substring method is used to get a portion of the string. For example, you want to get the String "James" from the String "Mark James".

equals method works in similar with compareTo method but they have a different return types. When I say return type it is a data type that is returned by the method like int, boolean, string, etc....

charAt method is the opposite of indexOf method. charAt will return the character at a specified index. For example, String name = "name"; System.out.print(name.charAt(1)); //this will display the letter 'a'.

 trim method is used to remove white-spaces from the beginning and ending of a string. For example, " name " will be trimmed to "name".

valueOf method is used to convert any data type passed in its parameter to String.

Here is a complete package project for Learning some String Methods.

No comments:

Post a Comment