在C#中保持大小写不变的情况下替换文本 [英] Replace text while keeping case intact in C#
问题描述
我需要使用一组句子来进行替换,例如:
I have a set of sentences i need to use to do a replace, for example:
abc => cde
ab df => de
...
我有一条文本可以在哪里进行更改. 但是我没有办法事先知道上述文本的情况. 因此,例如,如果我有:
And i have a text where to make the changes. However i have no way to know beforehand case of said text. So, for example, if i have:
A bgt abc hyi. Abc Ab df h
我必须更换并得到:
A bgt cde nyi. Cde De h
或者尽可能接近,即保持大小写
Or as close to that as possible, i.e. keep case
编辑:由于我对此感到困惑,我将尝试澄清一下:
As i am seeing to much confusion about this i will try to clarify a bit:
我要问的是更换后保持大写的方法,但我认为这样做不太顺利(不能很好地解释thaat的含义),所以我将使用真实的单词给出一个更现实的示例.
I am asking about a way to keep caps after replacing and i don't think that passed through well (not well explained what thaat entails) so i will give a more realistic example using real words.
把它想像成一个福音,可以说用它们的同义词代替表达,所以如果我映射的话:
think of it like a gossary, replacing expressions by their sinonyms so to speak, so if i map:
didn't achieve success => failled miserably
然后,我将输入设置:
As he didn't achieve success, he was fired
我会得到
As he failled miserably, he was fired
但是,如果没有大写,则失败,如果成功或成功被大写,那么不幸的是,如果有超过1个字母大写,那么对应的大写字母会
but if didn't was capitalized, so would failled, if achieve or success was capitalized, so would miserably, if any had more than 1 letter capitalized, so would it's counterpart
我的主要可能性是(我真的很想考虑的问题)
My main possibilities are (ones i really want to take into cosideration)
- 只有第一个单词的第一个字母大写
- 每个单词只有第一个字母大写
- 所有字母大写
如果我能处理已经可以适应的这三个条件,那是我认为-比较简单的方法-当然,如果可用的话,更深入的解决方案会更好
If i can handle those three that would be acceaptable already i guess - it's the easyer ones - of course a more in depth solution would be better if availlable
有什么想法吗?
推荐答案
不确定该方法的效果如何,但这是我想出的:
Not sure how well this will work, but this is what I came up with:
string input = "A bgt abc hyi. Abc Ab df h";
Dictionary<string, string> map = new Dictionary<string, string>();
map.Add("abc", "cde");
map.Add("ab df", "de");
string temp = input;
foreach (var entry in map)
{
string key = entry.Key;
string value = entry.Value;
temp = Regex.Replace(temp, key, match =>
{
bool isUpper = char.IsUpper(match.Value[0]);
char[] result = value.ToCharArray();
result[0] = isUpper
? char.ToUpper(result[0])
: char.ToLower(result[0]);
return new string(result);
}, RegexOptions.IgnoreCase);
}
label1.Text = temp; // output is A bgt cde hyi. Cde De h
编辑 阅读修改后的问题后,这是我的修改后的代码(事实证明这与@Sephallia的代码类似..,并且具有类似的变量名lol)
EDIT After reading the modified question, here's my modified code (it turns out to be similar steps to @Sephallia's code.. and similar variable names lol )
现在的代码有点复杂..但是我认为还可以
The code now is a bit more complicated.. but I think it's ok
string input =
@"As he didn't achieve success, he was fired.
As he DIDN'T ACHIEVE SUCCESS, he was fired.
As he Didn't Achieve Success, he was fired.
As he Didn't achieve success, he was fired.";
Dictionary<string, string> map = new Dictionary<string, string>();
map.Add("didn't achieve success", "failed miserably");
string temp = input;
foreach (var entry in map)
{
string key = entry.Key;
string value = entry.Value;
temp = Regex.Replace(temp, key, match =>
{
bool isFirstUpper, isEachUpper, isAllUpper;
string sentence = match.Value;
char[] sentenceArray = sentence.ToCharArray();
string[] words = sentence.Split(' ');
isFirstUpper = char.IsUpper(sentenceArray[0]);
isEachUpper = words.All(w => char.IsUpper(w[0]) || !char.IsLetter(w[0]));
isAllUpper = sentenceArray.All(c => char.IsUpper(c) || !char.IsLetter(c));
if (isAllUpper)
return value.ToUpper();
if (isEachUpper)
{
// capitalize first of each word... use regex again :P
string capitalized = Regex.Replace(value, @"\b\w", charMatch => charMatch.Value.ToUpper());
return capitalized;
}
char[] result = value.ToCharArray();
result[0] = isFirstUpper
? char.ToUpper(result[0])
: char.ToLower(result[0]);
return new string(result);
}, RegexOptions.IgnoreCase);
}
textBox1.Text = temp;
/* output is :
As he failed miserably, he was fired.
As he FAILED MISERABLY, he was fired.
As he Failed Miserably, he was fired.
As he Failed miserably, he was fired.
*/
这篇关于在C#中保持大小写不变的情况下替换文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!