Saturday 21 June 2014

replaceFirst (String regex, String replacement)

public String replaceFirst(String regex, String replacement)
Replaces the first substring of this string that matches the given regular expression with the given replacement.

class StringReplaceFirst{
 public static void main(String args[]){
  String s1 = "aaab Krishna aab";
  String s2;
  
  s2 = s1.replaceFirst("ab", "Hi");
  
  System.out.println("s1 = " + s1);
  System.out.println("s2 = " + s2);
 }
}

Output
s1 = aaab Krishna aab
s2 = aaHi Krishna aab





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment