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…

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…

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-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…

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…

RegionMatches

Java-RegionMatches

RegionMatches The regionMatches () method of String compares a specific region inside a string with another specific region in another string. There is an overloaded form that allows you to ignore case in such comparisons. It has two forms: For both versions, ‘toffset’ specifies the index at which the region begins within the invoking String object. … Read more…