Java String replace first digit matches

Introduction

The following table lists the predefined Java Regular expression character classes:

CharacterMatches
\d any digit
\D any non-digit
\w any word character
\W any nonword character
\s any white-space character
\S any non-whitespace character
public class Main {
  public static void main(String[] args) {
    String firstString = "in 5333 stars *****";
     //from   w ww  .  java  2  s .  c  om
    System.out.printf("Original String 1: %s\n", firstString);

    firstString = firstString.replaceFirst("\\d", "&&&");

    System.out.printf(firstString);
  }
}



PreviousNext

Related