1

I need to change the string by decreasing the number by one e.g. 011 then 010 etc. if 009 then 008.

However, I cannot think of the ways to do this thing please help me:

<img width="188" height="307" src="File1.files/image006.png" alt="NNMF_Input.png" v:shapes="image_x0020_33" />
<img width="506" height="200" src="File1.files/image014.png" v:shapes="image_x0020_1" />
<img width="506" height="411" src="File1.files/image016.png" v:shapes="image_x0020_2" />
<img width="515" height="179" src="File1.files/image018.png" v:shapes="image_x0020_3" />

Here, I want to change files/image006.png to files/image005.png and change say files/image010.png to files/image009.png.

P.S. They are all in strings! not HTML tags in fact

11
  • 2
    One Way is: a). Convert String to Integer, b). Decrement Integer, c). Convert back to String. Commented Feb 6, 2014 at 7:27
  • Yeah but the thing is that I can do that but the problem is I have to extract the part of the string and then put it back to where it was.. and it is 000 thing so it is not just 18 17 thing. Commented Feb 6, 2014 at 7:28
  • And use DecimalFormat to get the result formatted just the way you want it. Commented Feb 6, 2014 at 7:31
  • That is unsatisfactory since I want to automate this (not just them but say 100 of them) Commented Feb 6, 2014 at 7:32
  • 1
    Do you really want to do that in Java or did you mean Javascript? Asking because of HTML code. Commented Feb 6, 2014 at 7:36

4 Answers 4

6

try regex

    Matcher m = Pattern.compile("(?<=/image)\\d{3}").matcher(str);
    StringBuffer sb = new StringBuffer();
    while(m.find()) {
        m.appendReplacement(sb, String.format("%03d", Integer.parseInt(m.group()) - 1));
    }
    m.appendTail(sb);
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, I am pretty new to regex. Could you please tell me how I would get just the File1.files/image006.png from an img tag using Regex?
@user3278450 You can find the basic Regex tutorial here : vogella.com/tutorials/JavaRegularExpressions/article.html
mmm.. I do not know how to add more rules with the current regex in compile for example, ^"src=" .png"$ (but I want to exclude them ..please help me!)
0
int i = Integer.parseInt("011");
System.out.format("%03d", i-1);

Comments

0

Just for funny - scala solution :)

scala> def increment(str:String) = str.split("[^0-9]").
filter( s => s.nonEmpty && s.length >1).
foldLeft(str)((acc,curr) => acc.replace(curr, {val res = (curr.toInt+1).toString;Range(0,curr.length - res.length).
foldLeft(res)((acc,curr)=> "0"+acc) }))
increment: (str: String)String

scala> increment("File1.files/image014.png")
res10: String = File1.files/image015.png

Comments

0
String str = "000110";

// Get the last index and add 1 to determine number of leading zeros
int i = str.lastIndexOf('0') + 1;

// Subtract or do any math on the number you want
int newNumber = Integer.parseInt(str) - 1;

// Format the new string with leading zeros
String newString = String.format("%0" + i + "d", newNumber);

// See the new string
System.out.println(newString);

EDIT

To answer your edited question:

String str = "image0110";

// Get the number (above example 0110) from original string using the last character 'e 'from 'image'
str = str.substring(str.lastIndexOf('e') + 1);

// Get how many leading zeros are in there
int i = str.lastIndexOf('0') + 1;

// Do the math
int newNumber = Integer.parseInt(str) - 1;

// Form the new string starting with 'image' and leading zeros
String newString = "image" + String.format("%0" + i + "d", newNumber);

System.out.println(newString);

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.