Java-Get Position

Get Position The indexOf( ) method provides the position of a specific character in the given string. For example, in the following program, the indexOf( ) method will return ‘2‘ as the answer. This method has returned the position of first occurrence of the specified character. There is one more method known as the lastIndexOf( … Read more…

Java-Trimming

Trimming To remove whitespace from the beginning and end of a string (but not the interior) you can use the trim( ) method. Program Program Source public class Javaapp { public static void main(String[] args){ String str1 = new String(” haj sof “); System.out.println(str1); System.out.println(“str1.length = “+str1.length()); str1 = str1.trim(); System.out.println(str1); System.out.println(“str1.length = “+str1.length()); } … Read more…

Java-Startswith and Endswith

Startswith and Endswith The String class provides two more methods that are specialized versions of the equals( ) method. The startWith( ) method enables us to check whether a given string str1 starts with another string str2. For example, in the following program, the startWith( ) method will return true as the string “hajsoftutorial” starts … Read more…

Java-RegionMatches

RegionMatches The String class provides one more specialized method for comparison. It is regionMatches( ) method. This method enables us to compare a specific region inside a string with another specific region in another string. For example, in the following program, a substring of length ‘4’ starting from the position ‘3’ in the String object … Read more…

Java-Comparing Length

Comparing Length It should be carefully noted here equals( ), equalsIgnoreCase( ) methods enable us to check whether two strings are identical or not. However, in some situations, we may wish to test whether one string is greater than, equal to or less than another string. The compareTo( ) method of the String class is … Read more…

Java-Extracting Characters

Extracting Characters If we wish to extract more than one character at a time from a string, we have to use the getChars( ) method. For example, in the following program, the characters staring from the position ‘5’ and ending with the position ’16-1′ will be extracted and then stored in the character array ‘str1′ … Read more…