How can i find different characters in the strings at same positions? Ex:
String string1 = "Anand has 2 bags and 4 apples";
String n = /* ??? */;
String n2 = /* ??? */;
String string2 = "Anand has " + n + " bags and " + n2 + " apples";
I want n = "2" and n1 = "4".
Please let me know how we can do this? (Space added between words only for clarity purpose . But i can not use Space as delimiter)
You can use the StringTemplate class that i developed (I’d developed a URITemplate class to match restlike uris but have modified it to use strings as well)
Compile this and test as follows:
This is the output
The matches(String) returns the map containing the template variable names and values.
This class can be used for matching any string with any number of vars. Its liscensed apache2
If your input string contains regex characters, you will have to escape them:
Note that you need more accurate regexps for conditions where input string already has characters like “\$”