JAVA – string methods – 2

This the second post for Java string methods. In this post I will detail more functions and its functionalities. 

matches()

This method tests string against given regular expression. This method returns true only if this string matches the given regular expression.

Syntax:

public boolean matches(String regex)

regex  the regular expression.

Example:

public class Sample {
	public static void main(String args[]) {
		String Str = new String("Hello Visitor in SamiElkady.com");
		System.out.println(Str.matches("(.*)Sami(.*)"));
		System.out.println(Str.matches("^Hello(.*)"));
		System.out.println(Str.matches("^H[a-z]+(.*)"));
		System.out.println(Str.matches("^([A-Za-z]+)"));
	}
}

Result:

true
true
true
false

regionMatches()

This method has two different formats with different inputs (pramters) which can be used to test if two string regions are equal. It returns true if the specified subregion of this string matches the specified region of the string argument.

Syntax:

public boolean regionMatches(int i, string str, int o, int len);

OR

public boolean regionMatches(boolean ign, int i, String str, int o, int len);

i  the starting offset of the subregion in this string.

str  the string argument.

o  the starting offset of the subregion in the string argument.

len  the number of characters to compare.

ign  if true, ignore case when comparing characters.

Example:

public class sample{
   public static void main(String args[]){
      String Str1 = new String("Hello Visitor in SamiElkady.com");
      String Str2 = new String("SamiElkady");
      String Str3 = new String("SAMIELKADY");
      System.out.println(Str1.regionMatches(17, Str2, 0, 10));
      System.out.println(Str1.regionMatches(17, Str3, 0, 10));
      System.out.println(Str1.regionMatches(true, 17, Str3, 0, 10));
   }
}

Result:

true
false
true

replace()

This method searches for a character and repace it with new character creating new string by replacing all occurrences of oldChar in this string with newChar.

Syntax:

public String replace(char oldChar, char newChar);

oldChar  the old character to be replaced.

newChar  the new character.

Example:

public class Sample{
   public static void main(String args[]){
      String Str = new String("Hello Samy");
      System.out.println(Str.replace('y', 'i'));
   }
}

Result:

Hello Sami

replaceAll() 

This method replaces each substring of this string that matches the given regular expression with the given replacement.

Syntax:

public String replaceAll(String regex, String str);

regex  the regular expression to which this string is to be matched.

str  the string which would replace found expression.

Example:

public class Sample{
   public static void main(String args[]){
      String Str = new String("Hello Sami");
      System.out.println(Str.replaceAll("(Sami)", "John" ));
   }
}

Result:

Hello John

replaceFirst()

This method replaces the first substring of this string that matches the given regular expression with the given replacement.

Syntax:

public String replaceFirst(String regex, String replacement);

regex  the regular expression to which this string is to be matched.

replacement  the string which would replace found expression.

Example:

public class Sample{
   public static void main(String args[]){
      String Str = new String("Hello John and John");
      System.out.println(Str.replaceFirst("John", "Sami" ));
   }
}

Result:

Hello Sami and John