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

5
  • 5
    "maybe there is an easy regex command". There is. What did you try? Or are you asking us to write the regex for you, without doing any research on your own? Down-voted for lack of research. Commented Sep 12, 2016 at 18:05
  • stackoverflow.com/questions/4643142/… similar Commented Sep 12, 2016 at 18:05
  • 1
    regex is your friend... use a pattern for that Commented Sep 12, 2016 at 18:05
  • How many digits are allowed to be in the beginning Commented Sep 12, 2016 at 18:07
  • use this as a hint...stackoverflow.com/q/11232801/982161 Commented Sep 12, 2016 at 18:07

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

Comments

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);
}

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.