Question

I have the following information

String templateString = "I am %NAME% and I live in %PLACE%"; 

String inputString = "I am John Doe and I live in New York";

I need to write a function which will take in the above 2 strings and return a HashMap of pairs.

HashMap<String,String> parseInputFromTemplate(templateString, inputString) {

  // Magic 
  return result;
}

The result will have

%NAME% (key)  , John Doe (value)

%PLACE% (key) , New York (value)

Any pointers would be appreciated.

Was it helpful?

Solution

You can use regular expressions to extract what you need. Alternatively you can use the split() method to split on "%". Every other string in the resulting array would be a template var. The others would be the static strings to discard in the inputString.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top