-5

I want increase the numbers in a string.

For example; I want to modify

"HTML[0].BODY[0]/UL[1]/LI[10]/A[1]"

to

"HTML[1].BODY[1]/UL[2]/LI[11]/A[2]"

Note: I accept the answer, but I want to add my tryings; replace("8", "9").replace("7", "8").replace("6", "7").replace("5", "6").replace("4", "5").replace("3", "4").replace("2", "3").replace("1", "2").replace("0", "1") and I realize that 9 could not change so I tried

for(int i=0;i<s.length();i++){
        if (Character.isDigit(s.charAt(i)))
        {
            int a=Integer.parseInt(s, s.charAt(i))+1;
            s.charAt(i)=(char)a;
            System.out.println(s.charAt(i));

        }
        }

This did not work also.. second note: Generally, I tried to write all codes, and due to points I get an warning. And I did not want to write part of codes

3
  • 1
    you are wrong. and biased. I tried everything, but I could not post codes due to my points Commented Nov 29, 2014 at 17:07
  • 1
    There's no restriction on posting your code, no matter your points. So you didn't even try to post your code... Commented Nov 29, 2014 at 17:08
  • there is a restriction. Commented Nov 29, 2014 at 17:10

1 Answer 1

1

There are multiple ways to do this. I'll share one:

String input = "HTML[0].BODY[0]/UL[1]/LI[10]/A[1]";

Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(input);
StringBuffer sb = new StringBuffer();
while (m.find()) {
    m.appendReplacement(sb, String.valueOf(Integer.parseInt(m.group()) + 1));
}
m.appendTail(sb);

String output = sb.toString();

System.out.println(output);
Sign up to request clarification or add additional context in comments.

4 Comments

I don't know if doing other people's work is a good answer...
it is a good answer. and among all answers to same type of questions, it is the only one that answers detect and modify the digit in a string
Hey @ydmpcn I saw your other question. Already have long since that question. So I believe you when you say you tried many things. By the way, you can also upvote in the answer. ;)
Yes I tried this also:) But it requires 15 points

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.