Lets say I have a string of a.b.c.d. How do I write a method which will transform that string into abc.d ? Or is there any method available implementation out there ?
What I have tried so far
int dotPlacing = propertyName.lastIndexOf(".");//12
String modString = propertyName.replace(".", "");
modString = modString.substring(0, dotPlacing-1) + "."+modString.substring(dotPlacing-1);
I am using that for writing Hibernate criteria. It works for user.country.name but not for user.country.name.ss. Havent tried for any other strings.
You can extract substring form 0 to lastIndexOf('.'). In this substring replace all . to empty string. After that merge with subtring (from lastIndexOf . to end).
Something like:
String theString = "a.b.c.d";
String separator = ".";
String replacement = "";
String newString = theString.substring(0, theString.lastIndexOf(separator)).replaceAll(separator , replacement).concat(theString.substring(theString.lastIndexOf(separator)));
Assert.assertEquals("abc.d", newString);
String start = "a.b.c.d.wea.s";
String regex = "\\.(?=.*\\.)";
String end = start.replaceAll(regex, "");
System.out.println(end);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With