C#用另一个字符串替换字符串中的字符 [英] C# replace characters in string by another string
问题描述
我的字符串包含:
A = B / B = C / C = D ... / Z = A
示例:`for( int i = 0 ; i> myString.lenght; i ++)
{
OriginalString = OriginalString.Replace ...`bla bla
我希望 替换Chars 这个args我怎么能做这个 ??
谢谢。
将参数拆分为列表或表格,然后一次应用一个。您还需要注意,替换不会破坏已经应用的替换。例如,如果您有H = D / A = Q / D = Z,那么它会将所有H字符转换为Z.
您可以使用包含替换模式的字典。
然后你必须遍历这个词典
并调用字符串替换方法。
字典<字符串,字符串> replacementDictionary = new Dictionary< string,string>();
replacementDictionary.Add( H, D);
replacementDictionary.Add( E, A);
replacementDictionary.Add( L, Q);
replacementDictionary.Add( O, Y);
string text = 你好跨度>;
foreach ( var 条目 in replacementDictionary)
{
text = text.Replace(entry.Key,entry.Value);
text = text.Replace(entry.Key.ToLower(),entry.Value.ToLower());
}
MessageBox.Show(text);
TheRealSteveJudge的解决方案原则上是正确的,但它没有真的告诉你如何带你的args
字符串来构建replacementDictionary
更糟糕的是它遭受了replacementDictionary
值的迭代处理,给出了Richard MacCutchan指出的替换级联。
如果你的替换都是单个字符到单个字符然后replacementDictionary
应该是:
字典< char,char> replacementDictionary = new 字典< char,char>();
然后你可以使用LINQ来映射text
生成输出:
string output = string .Join( string .Empty,text.Select(c = > {
char rep;
return replacementDictionary.TryGetValue(c, out rep)?rep:c;
}));
现在从args
填充replacementDictionary
:
首先你需要在'/'
上拆分args
然后对于每个字符串,再次将'='拆分为key和value
将键/值对设置为replacementDictionary
。
如果是大写/小写独立,然后首先插入键/值对,两个值都为大写,然后重复两个值为小写。 (使用char.ToUpper(c)
或char.ToUpperInvariant(c)
作为适当的,同样,char.ToLower(c)中
/char.ToLowerInvariant(c)中
)
Hi, i have string that contains :
A=B/B=C/C=D.../Z=A
Example : `for (int i = 0; i > myString.lenght; i++)
{
OriginalString = OriginalString.Replace...` bla bla
And i want to replace Chars by this args how can i do this ??
Thanks.
Split the arguments into a list or table and then apply them one at a time. You will also need to take care that a replace does not corrupt one that has already been applied. For example if you have "H=D/A=Q/D=Z", then it would convert all the H characters to Z.
You could use a dictionary containing the replacement patterns.
Then you must loop through this dictionary
and call the string replace method.
Dictionary<string, string> replacementDictionary = new Dictionary<string, string>(); replacementDictionary.Add("H", "D"); replacementDictionary.Add("E", "A"); replacementDictionary.Add("L", "Q"); replacementDictionary.Add("O", "Y"); string text = "Hello"; foreach(var entry in replacementDictionary) { text = text.Replace(entry.Key, entry.Value); text = text.Replace(entry.Key.ToLower(), entry.Value.ToLower()); } MessageBox.Show(text);
The solution by TheRealSteveJudge is correct in principle, but it doesn't really tell you how to take yourargs
string to build thereplacementDictionary
and worse it suffers from the iterative processing of thereplacementDictionary
values giving a substitution cascade which Richard MacCutchan pointed out.
If your replacements are all single character to single character then thereplacementDictionary
should be:
Dictionary<char, char> replacementDictionary = new Dictionary<char, char>();
and then you can use LINQ to map through thetext
to generate the output:
string output = string.Join(string.Empty, text.Select(c => { char rep; return replacementDictionary.TryGetValue(c, out rep) ? rep : c; }));
Now to populate thereplacementDictionary
from theargs
:
first you need to splitargs
on the '/'
then for each of those strings, split again on the '=' into a "key" and "value"
set the key/value pairs into thereplacementDictionary
.
If this is upper/lowercase independent, then first insert the key/value pair with both values as uppercase then repeat with both of them as lowercase. (Use thechar.ToUpper(c)
orchar.ToUpperInvariant(c)
as apropriate, and likewise,char.ToLower(c)
/char.ToLowerInvariant(c)
)
这篇关于C#用另一个字符串替换字符串中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!