0

EDIT :

Goal : http://localhost:8080/api/upload/form/test/test

Is it possible to have some thing like `{a-b, A-B..0-9}` kind of pattern and match them and replace with value.

i have following string

http://localhost:8080/api/upload/form/{uploadType}/{uploadName}

there can be any no of strings like {uploadType}/{uploadName}.

how to replace them with some values in java?

5
  • Make a separate string with name `uploadName' and concatenate it to your parent string Commented May 2, 2013 at 11:24
  • it is not static string and it is dynamic string, that is where i am struggling and blinking where to start Commented May 2, 2013 at 11:26
  • You already asked this question, why are you asking again? stackoverflow.com/questions/16335539/… Commented May 2, 2013 at 11:31
  • What exactly do you mean by dynamic? Do you mean there will not be a fixed number of components to replace? Commented May 2, 2013 at 11:41
  • Thanks only now this question is understood clearly Commented May 2, 2013 at 11:44

6 Answers 6

1

[Edited] Apparently you don't know what substitutions you'll be looking for, or don't have a reasonable finite Map of them. In this case:

Pattern SUBST_Patt = Pattern.compile("\\{(\\w+)\\}");
StringBuilder sb = new StringBuilder( template);
Matcher m = SUBST_Patt.matcher( sb);
int index = 0;
while (m.find( index)) {
    String subst = m.group( 1);
    index = m.start();
    //
    String replacement = "replacement";       // .. lookup Subst -> Replacement here
    sb.replace( index, m.end(), replacement);
    index = index + replacement.length();
}

Look, I'm really expecting a +1 now.


[Simpler approach] String.replace() is a 'simple replace' & easy to use for your purposes; if you want regexes you can use String.replaceAll().

For multiple dynamic replacements:

public String substituteStr (String template, Map<String,String> substs) {
    String result = template;
    for (Map.Entry<String,String> subst : substs.entrySet()) {
        String pattern = "{"+subst.getKey()+"}";
        result = result.replace( pattern, subst.getValue());
    }
    return result;
}

That's the quick & easy approach, to start with.

Sign up to request clarification or add additional context in comments.

8 Comments

i am sorry to ask what will be in this Map<String,String>
@Anto Maps all have unique keys and values attached to it. So since the key is the first, that is the one you want to replace, and the other is the value you want to replace it by. For example :Map<keyToReplace,valueOftheReplacer>
if i know what key to replace that there is no need for me to ask this question here Lyrion. I am sorry
Thanks @Anto. I've actually used similar technique in commercial software.. it's been popular with clients.
Hi Thomas this is also the expected answer, thanks for that. Thomas could your give me your email id please.
|
0

You can use the replace method in the following way:

    String s = "http://localhost:8080/api/upload/form/{uploadType}/{uploadName}";
    String typevalue = "typeValue";
    String nameValue = "nameValue";
    s = s.replace("{uploadType}",value).replace("{uploadName}",nameValue);

Comments

0

You can take the string that start from {uploadType} till the end. Then you can split that string using "split" into string array. Were the first cell(0) is the type and 1 is the name.

Comments

0

Solution 1 :

String uploadName = "xyz";
String url = "http://localhost:8080/api/upload/form/" + uploadName;

Solution 2:

String uploadName  = "xyz";
String url = "http://localhost:8080/api/upload/form/{uploadName}";
url.replace("{uploadName}",uploadName );

Solution 3:

String uploadName  = "xyz";
String url = String.format("http://localhost:8080/api/upload/form/ %s ", uploadName);

Comments

0
String s = "http://localhost:8080/api/upload/form/{uploadType}/{uploadName}";
String result = s.replace("uploadType", "UploadedType").replace("uploadName","UploadedName");

EDIT: Try this:

String r = s.substring(0 , s.indexOf("{")) + "replacement";

4 Comments

{uploadType}/{uploadName} this can be anystring but the common is {} that is why i am blinking
That is not a static string, it is a dynamic string. see my comment and edit as well, thanks for the post
@Anto See my answer using indexOf().
this will replace everything and will remain wit one value like http://localhost:8080/api/upload/form/replacement for given example string
-1

The UriBuilder does exactly what you need:

UriBuilder.fromPath("http://localhost:8080/api/upload/form/{uploadType}/{uploadName}").build("foo", "bar");

Results in:

http://localhost:8080/api/upload/form/foo/bar

2 Comments

i have already commented that it is not static string and dynamic:(
If you mean to say that there will not be a fixed number of parameters to add, that is not an issue. If it is dynamic then you will be iteratively replacing the components anyway, look at the UriBuilder methods in the link and see how you can put the URI together dynamically before assuming this solution is only applicable to static strings and downvoting - there's a reason this got 3 upvotes when you asked this very question just minutes ago.

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.