문제

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