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).

1
  • 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"? Commented Mar 8, 2014 at 0:57

4 Answers 4

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

3 Comments

this is probably what the OP actually wanted
Using regex is a cleaner solution.
+1 I like this answer too, but after reading up on Regex, I find that's what I was looking for. Thanks for your answer though.
2
for(int i = 0; str.contains("<< a"+i+" >>"), i++) {
    str = str.replace("<< a"+i+" >>", a[i]);
}

1 Comment

+1 for the looping version of my answer. Looks clean except it needs to be str = str.replace...
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);

5 Comments

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 >>
@AftabHussain I have updated my answer once again since you don't know the number of variables.
I'm guessing 'a' in String.format is the array. Right?
This is not as flexible as other answers because it forces <<a1>> and <<a2>> to always be in that order. If this is used for localizing text, enforcing the order is not an option.
+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.
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.

1 Comment

I had no clue what Regex was, but after reading and watching a tutorial, this is exactly what I need right now. Thanks

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.