I am having two string objects
String uri= employee/details[0].address[1].telephone
String uriPath=/employee/details/#/address/#/phone
And i need to update uriPath
to /employee/details/0/address/1/phone
. i need to go along uri
and get the numbers within []
and replace #
inside uriPath
in order.
CodePudding user response:
Why not just replace the '[', ']' and '.' with a '/'?
String uriPath = "/" uri.replace('[','/').replace(']','/').replace('.','/')
CodePudding user response:
Using String#replaceAll
:
String uri = "employee/details[0].address[1].telephone";
uri = uri.replaceAll("\\[(\\d )\\]\\.", "/$1/");
System.out.println(uri); // employee/details/0/address/1/telephone
CodePudding user response:
It sounds like you need to parse one string and replace the other.
Scanner scan = new Scanner(uri);
while( scan.hasNextInt() ){
int a = scan.nextInt();
uriPath = uriPath.replaceFirst(Pattern.quote("#"), a);
}
I used a Scanner to grab the ints, but you could use a pattern matcher.
Pattern p = Pattern.compile( "\\[(\\d )\\]" );
Then do a loop.
Matcher m = p.matcher( uri );
while(m.find()){
uriPath = uriPath.replaceFirst(Pattern.quote("#"), m.group(1));
}
That will look for [0] and grab the zero and replace # s sequentially.