0

I am getting a String, for ex.: "The user << a1 >> is << a2 >> 50 years old."
And I also have an array with the data that needs to go in the String! For ex. a[0]= "John" ; a[1]= "30"
So, for this example, I would like to replace << a1 >> with John and << a2 >> with 30.

The only thing I was able to find was the following question: How to replace a set of tokens in a Java String?, but to be honest I did not understand anything and wasn't sure if that's what I'm really looking for.

So, is that really what I need to work with? If it is, I'll go read some tutorials.
Thanks in advance.

EDIT: NOTE: I don't have control over the String coming in though. So it will be exactly the way I typed it. All variables are in the form << a0 >> and the number of variable is unknown (there might even be 10 variables).

Community
  • 1
  • 1
Aftab Safdar
  • 653
  • 5
  • 19
  • How do you connect a gap's name to its value? Is it simply "the first index in the value-array goes into the first gap in the input string"? – aliteralmind Mar 08 '14 at 00:57

4 Answers4

4
String.format("The user %s is %s 50 years old.", a[0],a[1]);

If you must use <<a1>> and <<a2>> then something like this instead...

String s = "The user <<a1>> is <<a2>> years old.";
String output = s.replace("<<a1>>", a[0]).replace("<<a2>>", a[1]);
Ted Bigham
  • 4,237
  • 1
  • 26
  • 31
1

If you don't have control over the string, you can replace instances of << a# >> as follows:

String yourString = "The user << a1 >> is << a2 >> years old.";
String transformedString = yourString.replaceAll("<< a\d >>", "%s");

This will replace any instance of << a# >> with %s. That will change your "The user << a1 >> is << a2 >> years old." string to "The user %s is %s years old" which can be used with the string formatter.

Then use the string formatter as follows.

String finalString = String.format(transformedString, a);
AxiomaticNexus
  • 6,190
  • 3
  • 41
  • 61
  • I don't have control over the String coming in though. So it will be exactly the way I typed it. All variables are in the form `<< a0 >>` – Aftab Safdar Mar 08 '14 at 00:35
  • @AftabHussain I have updated my answer once again since you don't know the number of variables. – AxiomaticNexus Mar 08 '14 at 01:01
  • I'm guessing 'a' in `String.format` is the array. Right? – Aftab Safdar Mar 08 '14 at 01:03
  • This is not as flexible as other answers because it forces <> and <> to always be in that order. If this is used for localizing text, enforcing the order is not an option. – Ted Bigham Mar 08 '14 at 23:03
  • +1 to you sir. Thanks for this answer, but I realized other stuff on the incoming String and after reading up on Regex, I'll go with that. – Aftab Safdar Mar 09 '14 at 03:30
1
for(int i = 0; str.contains("<< a"+i+" >>"), i++) {
    str = str.replace("<< a"+i+" >>", a[i]);
}
jpdymond
  • 1,517
  • 1
  • 8
  • 10
1

There's nothing stopping you from changing the format of your input string, before processing it further. It would give you more options.

That said, this uses a "reluctant" regex to replace all "gaps" as you require. It assumes that there's exactly one character before the index number, but the index can be multiple digits.

 import  java.util.regex.Pattern;
 import  java.util.regex.Matcher;
 /**
    <P>{@code java FillGaps}</P>
  **/
 public class FillGaps  {
    public static final void main(String[] ignored)  {

       String sRegex = "<< (.+?) >>";

       String sToSearch = "The user << a1 >> is << a2 >> years old.";
       String[] values = new String[] {"John", "30"};

       Matcher m = Pattern.compile(sRegex).matcher(sToSearch);
       StringBuffer sb = new StringBuffer();

       while(m.find())  {
          String gapName = m.group(1);
          String idxPlus1AsStr = gapName.substring(1, gapName.length());
          int arrayIdx = Integer.parseInt(idxPlus1AsStr) - 1;
          m.appendReplacement(sb, values[arrayIdx]);
       }
       m.appendTail(sb);

       System.out.println(sb);
    }
 }

Output:

[C:\java_code\]java FillGaps
The user John is 30 years old.
aliteralmind
  • 19,847
  • 17
  • 77
  • 108
  • I had no clue what Regex was, but after reading and watching a tutorial, this is exactly what I need right now. Thanks – Aftab Safdar Mar 09 '14 at 03:30