質問

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.

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top