C# 字符串替换为字典值
我在用字典中的值替换字符串中的单词时遇到了一些问题。下面是我当前代码的一个小示例:C# 字符串替换为字典值,c#,dictionary,C#,Dictionary,我在用字典中的值替换字符串中的单词时遇到了一些问题。下面是我当前代码的一个小示例: Dictionary<string, string> replacements = new Dictionary<string, string>() { {"ACFT", "AIRCRAFT"}, {"FT", "FEET"}, }; foreach(string s in replacements.Keys) { inputBox.Text = inputBox.
Dictionary<string, string> replacements = new Dictionary<string, string>()
{
{"ACFT", "AIRCRAFT"},
{"FT", "FEET"},
};
foreach(string s in replacements.Keys)
{
inputBox.Text = inputBox.Text.Replace(s, replacements[s]);
}
字典替换=新字典()
{
{“ACFT”,“飞机”},
{“英尺”,“英尺”},
};
foreach(replacements.Keys中的字符串s)
{
Text=inputBox.Text.Replace(s,replacements[s]);
}
当我执行代码时,如果文本框中有ACFT
,它将替换为AIRCRAFEET
,因为它在字符串中看到FT
部分。我需要以某种方式区分这个词,只替换整个词
例如,如果框中有ACFT
,则应将其替换为飞机
。如果框中有FT
,请将其替换为FEET
所以我的问题是,在替换单词时,如何只匹配整个单词
编辑:我希望能够使用和替换多个单词。使用if条件
foreach(string s in replacements.Keys) {
if(inputBox.Text==s){
inputBox.Text = inputBox.Text.Replace(s, replacements[s]);
}
}
修改问题后更新
string str = "ACFT FTT";
Dictionary<string, string> replacements = new Dictionary<string, string>()
{
{"ACFT", "AIRCRAFT"},
{"FT", "FEET"},
};
string[] temp = str.Split(' ');
string newStr = "";
for (int i = 0; i < temp.Length; i++)
{
try
{
temp[i] = temp[i].Replace(temp[i], replacements[temp[i]]);
}
catch (KeyNotFoundException e)
{
// not found..
}
newStr+=temp[i]+" ";
}
Console.WriteLine( newStr);
string str=“ACFT-FTT”;
字典替换=新字典()
{
{“ACFT”,“飞机”},
{“英尺”,“英尺”},
};
字符串[]temp=str.Split(“”);
字符串newStr=“”;
对于(int i=0;i
问题在于,您正在替换整个字符串中子字符串的每个实例。如果您只想替换“ACFT”或“FT”的整个、以空格分隔的实例,那么您需要使用String.Split()来创建一组令牌
例如:
string tempString = textBox1.Text;
StringBuilder finalString = new StringBuilder();
foreach (string word in tempString.Split(new char[] { ' ' })
{
foreach(string s in replacements.Keys)
{
finalString.Append(word.Replace(s, replacements[s]));
}
}
textBox1.Text = finalString.ToString();
我在这里使用了StringBuilder,因为串联每次都需要创建一个新字符串,这在很长一段时间内效率非常低。如果您希望进行少量的连接,则可以使用字符串
请注意,在您的设计中有一点瑕疵-如果您有一个KeyValuePair,其值与字典迭代中稍后出现的键相同,则替换将被覆盖。我认为您可能需要替换inputText中的最大长度子字符串
int maxLength = 0;
string reStr = "";
foreach (string s in replacements.Keys)
{
if (textBox2.Text.Contains(s))
{
if (maxLength < s.Length)
{
maxLength = s.Length;
reStr = s;
}
}
}
if (reStr != "")
textBox2.Text = textBox2.Text.Replace(reStr, replacements[reStr]);
int maxLength=0;
字符串rest=“”;
foreach(replacements.Keys中的字符串s)
{
if(textBox2.Text.Contains)
{
如果(最大长度
替换单词时,如何仅匹配整个单词
使用正则表达式(正如David Pilkington所建议的那样)
字典替换=新字典()
{
{“ACFT”,“飞机”},
{“英尺”,“英尺”},
};
foreach(replacements.Keys中的字符串s)
{
var pattern=“\b”+s+“\b”;//匹配单词边界
Text=Regex.Replace(inputBox.Text,pattern,replacements[s]);
}
但是,如果您可以控制设计,我更愿意使用键,如
“{ACFT}”
,“{FT}”
(具有明确的边界),因此您可以将它们与字符串一起使用。替换 这是一种非常时髦的方法
首先,您需要使用正则表达式(Regex
),因为它具有用于匹配单词边界的良好内置功能
因此,代码的关键行是定义一个Regex
实例:
var regex = new Regex(String.Format(@"\b{0}\b", Regex.Escape("ACFT"));
\b
标记查找单词边界。Regex.Escape
确保,如果您的任何其他键具有特殊的Regex
字符,则它们将被转义
然后,您可以这样替换文本:
var replacedtext = regex.Replace("A FT AFT", "FEET");
您将得到replacedtext==“船尾一英尺”
现在,这里是最时髦的部分。如果您从当前词典开始,那么您可以定义一个函数,该函数将一次性完成所有替换
这样做:
Func<string, string> funcreplaceall =
replacements
.ToDictionary(
kvp => new Regex(String.Format(@"\b{0}\b", Regex.Escape(kvp.Key))),
kvp => kvp.Value)
.Select(kvp =>
(Func<string, string>)(x => kvp.Key.Replace(x, kvp.Value)))
.Aggregate((f0, f1) => x => f1(f0(x)));
不需要循环
作为一个精神检查,我得到了这个:
funcreplaceall("A ACFT FT RACFT B") == "A AIRCRAFT FEET RACFT B"
试试看,我已经编辑了你的标题。请看,“,其中的共识是“不,他们不应该”。他到目前为止只说了一个字。。看看他举了什么例子。。在此基础上我回答了。。若仔细阅读答案,那个么在此之前必须仔细阅读问题1.谢谢你们的建议,迪帕克。我本来应该在我原来的问题上说得更清楚一些;但是,如果我有多个单词,您的解决方案将不起作用。我认为您的列表将是Console.WriteLine(s.Replace(s,replacements[s])@DeepakSharma我误解了请求-现在编辑
inputBox.Text = funcreplaceall(inputBox.Text);
funcreplaceall("A ACFT FT RACFT B") == "A AIRCRAFT FEET RACFT B"