-3

I would like to remove all digits that come at the start of a string. I would just like to remove the digits at the start, not at the end or the middle.

Example: "123string67" to "string67"

Is there some function or regex I can use to easily do this?

mginn
  • 16,036
  • 4
  • 26
  • 54

3 Answers3

1

You can use group capture and ^\d*(.*) regex

Pattern pattern = java.util.regex.Pattern.compile("^\\d*(.*)")
Matcher matcher = pattern.matcher("123string67")
if (matcher.matches()) {
    String truncated = matcher.group(); //string67
}

Here is a regex explained

^    - start of string
\d*  - zero or more digit
 (   - group capture start
  .* - any symbol
 )   - group capture end
vsminkov
  • 10,912
  • 2
  • 38
  • 50
0

Here's a hint (without regex): (*you still need to consider the case where the string is empty or contains only digits)

int i=0;
while(isDigit(str.charAt(i))) {
    str.deleteCharAt(i);
}
PEF
  • 207
  • 1
  • 4
  • 11
0

You can use for ( i.. To i.string.lenght..) and control with ASCII number and delete digit number so you can replaced your string as you want i hope this helpful for you