1

Let's say i have a string like this: "/c1/client/{clientId}/field/{fieldId}/version/{versionId}/file/{filename}"

I want to replace all values inside curly brackets with the actual values, so the link would look like this:

"c1/client/Tudor/field/search/version/1/file/hello.txt".

How can i do that in a way that does not limit the number of parameters used? Because i have some requests with 4 parameters (like this one) and others with only one parameter, or none. What is the best way to do this?

Edit: I would need something like: Search string for any values between {}. If string contains {value}, take all {values} and replace with parameter.

Tudor
  • 199
  • 3
  • 14
  • you could go with a `Map parameterValueMap` in order to store the key <-> value relation you´d need and simply call a replace all for each `{keyInTheMap}` and could replace it with the repctive value – SomeJavaGuy Apr 19 '17 at 11:24
  • Possible duplicate of [Java Replacing multiple different substring in a string at once (or in the most efficient way)](http://stackoverflow.com/questions/1326682/java-replacing-multiple-different-substring-in-a-string-at-once-or-in-the-most) – Timothy Truckle Apr 19 '17 at 11:24
  • 1. is your clientid always going to be a value "Tudor"? 2. Or is the first {parameter} always "Tudor"? – aksappy Apr 19 '17 at 11:41
  • not necessarily. may have multiple values, but i can deal with that in another way. What i need is a way to replace {clientId} with Tudor, regardless of how many {parameters} i have. – Tudor Apr 19 '17 at 11:42

3 Answers3

0

You can parse @pathParameters and redirect to the address you create with spring @controller. If these are request as you wrote that is the right approach.

strash
  • 1,291
  • 2
  • 15
  • 29
0

In case of String:

var u = "/c1/client/{clientId}/field/{fieldId}/version/{versionId}/file/{filename}";
u.replace(/\{clientId\}/, "Tudor").replace(/\{fieldId\}/, "search").replace(/\{versionId\}/, 1).replace(/\{filename}/, "hello.txt");
Ashish Kumar
  • 2,991
  • 3
  • 18
  • 27
  • Yes but in another request, it will not be {clientId}, will be {createClient} for example... i am looking for a method that takes everything that is inside curly brackets and replace them with the actual values. – Tudor Apr 19 '17 at 11:28
0

You can try this

String str = "/c1/client/{clientId}/field/{fieldId}/version/{versionId}/file/{filename}";
Map<String, String> map = new HashMap<String, String>();
map.put("clientId", "Tudor");
map.put("fieldId", "search");
map.put("versionId", "1");
map.put("filename", "hello.txt");
for (Map.Entry<String, String> entry : map.entrySet()) {
str = str.replace("{" + entry.getKey() + "}", entry.getValue());
}
String newStr = str.substring(1);
System.out.println(newStr);
user3441151
  • 1,880
  • 6
  • 35
  • 79
  • and what happens if i only have three parameters, with other names? – Tudor Apr 19 '17 at 11:51
  • @Tudor You know the mapping for your all parameters? – user3441151 Apr 19 '17 at 11:52
  • Yes, i will have a value for each parameter there. the thing is, i want something to search the string for values between curly brackets. If found, then replace {parameter} with his value. – Tudor Apr 19 '17 at 11:54
  • @Tudor So you have to only map those parameters? – user3441151 Apr 19 '17 at 11:55
  • I have to map parameters inside curly brackets. So i won;t have only those parameters, i need something like: search for {parameter}, if it exists replace {parameter} with value. – Tudor Apr 19 '17 at 12:19
  • @Tudor Have you tried this? By using `str.replace("{" + entry.getKey() + "}", entry.getValue());` your `{parameter}` replace with value that you defined in your mapping. – user3441151 Apr 19 '17 at 12:34
  • Ok, then how do i extract from the initial string the clientId, fieldId, versionId and filename that i put in the hashmap? because i have 200 requests to test, and i don;t want to manually extract those values for every request. – Tudor Apr 19 '17 at 12:36
  • @Tudor You must have to map your all values and for this you can use `HashMap`. This is what I have done. Is this not your requirement? – user3441151 Apr 19 '17 at 12:40
  • @Tudor Is this helpful for you? – user3441151 Apr 19 '17 at 13:15
  • No, i just told you. I do not need to map all values. I need something that searches for any word that starts with { and ends with }, and then do the replacement. – Tudor Apr 19 '17 at 13:24
  • @Tudor How can they do replacement? You have to map those word Right? – user3441151 Apr 19 '17 at 13:27
  • @Tudor I don't know what you talking about the request. Can you show your full code? What you trying to do? – user3441151 Apr 19 '17 at 13:28