4

Is there any Java solution of replacing a digit in a String other than getting the digit using a matcher, increment it by one and replace it?

"REPEAT_FOR_4" will return "REPEAT_FOR_5" "REPEAT_FOR_10" will return "REPEAT_FOR_11"

I would like to do it in one line with regex and replace, not by recomposing the String as "REPEAT_FOR_" and add the number after incrementation. Thank you!

Later edit: I would like to know how to replace a number with the following one in a String.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
sonya
  • 229
  • 1
  • 3
  • 9
  • 1
    Yes, with `Matcher#appendReplacement` – Wiktor Stribiżew Oct 13 '16 at 14:09
  • 4
    Technically, you can't "replace" anything in a `String`, because objects of `String` are immutable. But you can create a new `String` containing the old prefix plus Integer.valueOf(suffix) + 1. – Smutje Oct 13 '16 at 14:10
  • Yes, I know. I will update my question. Generate a new String :) – sonya Oct 13 '16 at 14:10
  • You could use an IntStream, map it to an Integer, then produce a list of strings in one line of code. – mttdbrd Oct 13 '16 at 14:10
  • give an example of input and expected output – Pavneet_Singh Oct 13 '16 at 14:11
  • In this case, regex will only make the code longer, not shorter. – RealSkeptic Oct 13 '16 at 14:26
  • What is the pattern of the String? The number is always at the end, always present, The splitter is `_`? Could you enumerate more condition ? – AxelH Oct 13 '16 at 14:33
  • No, this only an example. I would like to know solutions for replace any number in a String with the following one. – sonya Oct 13 '16 at 14:41
  • @rianna, then update your example. This is not clear for me (and others too). You need to specify a pattern, or patterns of String. (or a number anywhere...) – AxelH Oct 13 '16 at 14:43
  • 1
    Thanks, Pavneet! Sorry for duplicating the questions. – sonya Oct 13 '16 at 14:48
  • don't be sorry , it was a good question though :) – Pavneet_Singh Oct 13 '16 at 15:02
  • don't be sorry , it was a good question though :) , you can try this too http://stackoverflow.com/questions/1453365/increment-digit-value-in-string , , plus the below given answers are also great examples too – Pavneet_Singh Oct 13 '16 at 15:10
  • Revising my comment from yesterday to use iterate() instead of generate().This does not satisfy the requirement of not recomposing the String, but it will generate any number of Strings in one line: List strings = IntStream.iterate(0, x -> ++x).limit(100) .mapToObj(x -> "REPEAT_FOR_" + Integer.toString(x)) .collect(Collectors.toList()); – mttdbrd Oct 14 '16 at 16:22
  • @rianna doesn't my answer resolve your problem? – cнŝdk Oct 26 '16 at 08:48

3 Answers3

0

I didn't use regex but here is the solution in one line. Considering your string remains the same.

public String getIncrementedString (String str){
return ("REPEAT_FOR_" + (Character.getNumericValue(str.charAt(11))+1));
}
Eddie Martinez
  • 13,582
  • 13
  • 81
  • 106
0

You can try this.

String ss = "REPEAT_FOR_4";
int vd = Integer.valueOf(ss.substring(ss.length() - 1));
String nss = ss.replaceAll("\\d",String.valueOf(vd+1));

System.out.println(nss);

output:

REPEAT_FOR_5

with regex: if the digit is not at the end of the string.

    String ss = "REPEAT_5_FOR_ME";
    Pattern p = Pattern.compile("\\d+");
    Matcher m = p.matcher(ss);
    m.find();
    String strb = m.group();
    int vd = Integer.valueOf(strb);
    String nss = ss.replaceAll("\\d",String.valueOf(vd+1));

    System.out.println(nss);

output:

REPEAT_6_FOR_ME

Base on the issue raised in the comments comments i think this solution with regex will help.

public static String convStr(String str){
    Pattern p = Pattern.compile("\\d+");
    Matcher m = p.matcher(str);
    m.find();
    String strb = m.group();
    int vd = Integer.valueOf(strb);
    return str.replaceAll("\\d",String.valueOf(vd+1));
  }
Seek Addo
  • 1,871
  • 2
  • 18
  • 30
  • But if the number is not on the end of the string, it will not work. I came up to this solution: String nrToBeReplaced = CharMatcher.DIGIT.retainFrom(value); int nextNr = Integer.valueOf(nrToBeReplaced) + 1; return value.replaceAll(value, String.valueOf(nextNr)); – sonya Oct 13 '16 at 14:38
  • @rianna i have updated my answer with regex and it solves the issue you raised – Seek Addo Oct 13 '16 at 14:52
0

Yes of course it's possible. using regex Pattern and Matcher, here's what you will need to do:

    String str = "REPEAT_FOR_4";
    Pattern p = Pattern.compile("([0-9]+)");
    Matcher m = p.matcher(str);
    StringBuffer s = new StringBuffer();
    while (m.find())
        m.appendReplacement(s, String.valueOf(1+ Integer.parseInt(m.group(1))));
    String updated = s.toString();
    System.out.println(updated);

This is a working Example that returns REPEAT_FOR_5 as output.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78