嗯,你可以使用正則表達式和匹配評價回調是這樣的:一旦你的
private string EvaluateMatchCallback(Match match) {
string templateInsert = match.Groups["value"].Value;
// or whatever
string replacedText = GetReplacementTextBasedOnTemplateValue(templateInsert);
return replacedText;
}
:
regex = new Regex(@"\{(?<value>.*?)\}",
RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture);
string replacedText = regex.Replace(<text>,
new MatchEvaluator(this.EvaluateMatchCallback));
而且你的評價回調會做這樣的事情正則表達式匹配值,你可以做一個區分大小寫的比較並返回正確的替換值。
編輯我覺得你試圖找到一個文本塊中的佔位符,而不是擔心外殼本身,如果你的模式一直有效,那麼你可以檢查前兩個字符佔位符本身會告訴你,你需要在替換表達式中使用的外殼:
string foo = "teMPLATE";
if (char.IsLower(foo[0])) {
if (char.IsLower(foo[1])) {
// first lower and second lower
}
else {
// first lower and second upper
}
}
else {
if (char.IsLower(foo[1])) {
// first upper and second lower
}
else {
// first upper and second upper
}
}
我仍然會使用正則表達式匹配替換佔位符,不過這只是我。
如果不是所有的字符/下限 - 只是跳過串occurence? – sll 2012-03-23 20:33:20
如果模板設置爲「駱駝案例」時尚,只需通過檢查前兩個字母來確定其餘部分......哦,geee ......我只是意識到 - 任何時候它只取決於一對前導字母 – Agzam 2012-03-23 20:35:27
這些只是4個案例嗎?或者你有更多的,例如'{tEmPlAte}',等等? – code4life 2012-03-23 20:39:31