C#用另一个字符串替换字符串中的字符 [英] C# replace characters in string by another string

查看:173
本文介绍了C#用另一个字符串替换字符串中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的字符串包含

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 your args string to build the replacementDictionary and worse it suffers from the iterative processing of the replacementDictionary values giving a substitution cascade which Richard MacCutchan pointed out.

If your replacements are all single character to single character then the replacementDictionary should be:

Dictionary<char, char> replacementDictionary = new Dictionary<char, char>();


and then you can use LINQ to map through the text 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 the replacementDictionary from the args:
first you need to split args on the '/'
then for each of those strings, split again on the '=' into a "key" and "value"
set the key/value pairs into the replacementDictionary.
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 the char.ToUpper(c) or char.ToUpperInvariant(c) as apropriate, and likewise, char.ToLower(c)/char.ToLowerInvariant(c))


这篇关于C#用另一个字符串替换字符串中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆