Lexicographical Comparison

Java-Lexicographical Comparison

Lexicographical Comparison The equals( ) and 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 useful in all such … Read more…

String to Char Array

Java-String to Char Array

String to Char Array If we wish to extract more than one character at a time from a string, we have to use the getChars( ) method: For example: The characters staring from the position ‘3’ and ending with the position ‘8-1′ will be extracted and then stored in the character array ‘chararray’ from the … Read more…

Java-Replacement (Another Example)

replaceAll The capability of replaceAll() makes the complex search of substrings possible within a string object. After a search, each matched substring is replaced with the given replacement. Program Program Source public class Javaapp { public static void main(String[] args){ String str1 = new String(“Java Pro Tutorials”); str1 = str1.replaceAll(“Pro”, “Programming”); String str2 = new … Read more…

Replacement in a String

Java-Replacement in a String

Replace Method If you want to replace all occurrences of one character or character sequence in the invoking string with another character or character sequence use the replace() method. The replace( ) method has two forms: In the first form, oldChar specifies the character to be replaced by the character specified by newChar. The resulting string is returned. … Read more…

Extracting Character and Concatenation

Java-Extracting Character and Concatenation

Extracting Character The charAt( ) method provides the character at the specified index position. The starting value of the index in the case of a string is zero. For example, in the following example, the character at position 3 in the string “ABCDEFG” is ‘D’. Thus, the str1.charAt(3) method will return the character ‘D’. Example: … Read more…

Java-IgnoreCase

IgnoreCase A minor variant of the equals( ) method, namely the equalsIgnoreCase( ) method is also available. This method ignores the case of each character in the strings during the comparison. For example, in the following program, the case of each character is ignored by the equalsIgnoreCase( ) method, this method will return the value … Read more…