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?

Java Questions
  • 7,813
  • 41
  • 118
  • 176

6 Answers6

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.

Thomas W
  • 13,940
  • 4
  • 58
  • 76
  • i am sorry to ask what will be in this `Map` – Java Questions May 02 '13 at 11:39
  • 1
    @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 – Lyrion May 02 '13 at 11:49
  • if i know what key to replace that there is no need for me to ask this question here Lyrion. I am sorry – Java Questions May 02 '13 at 11:51
  • Thanks @Anto. I've actually used similar technique in commercial software.. it's been popular with clients. – Thomas W May 03 '13 at 08:01
  • Hi Thomas this is also the expected answer, thanks for that. Thomas could your give me your email id please. – Java Questions May 03 '13 at 08:06
  • Hi Thomas not able to get you on java room, just wanted to ask few things, would you be able to spend few mins with me?. Sorry for late reply, away for lunch – Java Questions May 03 '13 at 08:54
  • @Anto http://chat.stackoverflow.com/rooms/29351/room-for-thomas-w-and-anto -- I'm interested in contracting, but busy working & don't really give general advice. – Thomas W May 03 '13 at 09:00
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29353/discussion-between-thomas-w-and-anto) – Thomas W May 03 '13 at 09:05
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);
Rahul Bobhate
  • 4,892
  • 3
  • 25
  • 48
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.

Orel Eraki
  • 11,940
  • 3
  • 28
  • 36
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";
Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
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);
Gaurav Arora
  • 17,124
  • 5
  • 33
  • 44
-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
Quetzalcoatl
  • 3,037
  • 2
  • 18
  • 27
  • i have already commented that it is not static string and dynamic:( – Java Questions May 02 '13 at 11:33
  • 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. – Quetzalcoatl May 02 '13 at 11:37