Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#替换为混合值_C#_Php - Fatal编程技术网

C#替换为混合值

C#替换为混合值,c#,php,C#,Php,我正在寻找的是一种在C中复制PHP str_替换()函数的方法 在PHPcode中,它看起来像: $string = 'This is my demo y string!'; $search_for = array("y","x","z"); $replace_with = array("1","2","3"); if( str_replace($search_for,$replace_with,$string) ) { $changed = true; }; 如果y更改为1,x更改为

我正在寻找的是一种在
C
中复制
PHP str_替换()
函数的方法

PHP
code中,它看起来像:

$string = 'This is my demo y string!';
$search_for = array("y","x","z");
$replace_with = array("1","2","3");
if( str_replace($search_for,$replace_with,$string) ) {
    $changed = true;
};
如果
y
更改为
1
x
更改为
2
等,如果有任何更改,则设置变量
changed
,或执行任何其他代码

如何在
C#
中执行相同的操作?
谢谢。

您可以像这样链接您的字符串:

var result = myString.Replace("y", "1").Replace("x", "2").Replace("z", "3");
这应该符合你的要求。如果您有带有替换项的阵列,另一种方法是:

var originalChar = new List<char> { '1', '2', '3' };
var replaceWith = new List<char> { 'x', 'y', 'z' };
originalChar.ForEach(x => myString = myString.Replace(x, replaceWith[originalChar.IndexOf(x)]));

您可以使用
LINQ
字典

var mappings = new Dictionary<char,char> { {'y', '1' }, { 'x', '2'} , {'z','3'}};

var newChars = inputString
                     .Select(x =>
                     {
                         if (mappings.ContainsKey(x)) 
                                return mappings[x];
                         return x;
                     })
                     .ToArray();

var output = new string(newChars);

bool changed = !(output == inputString);
var-mappings=newdictionary{{'y','1'},{'x','2'},{'z','3'};
var newChars=inputString
.选择(x=>
{
if(mappings.ContainsKey(x))
返回映射[x];
返回x;
})
.ToArray();
var输出=新字符串(newChars);
布尔改变了=!(输出==输入字符串);

这是一个简单的端口,尽管我认为最好使用“传统”方式C

string inputString=“这是我的演示字符串!”;
字符串[]搜索=新字符串[]{“y”、“x”、“z”};//或者char[]如果您愿意的话
字符串[]替换为=新字符串[]{“1”、“2”、“3”};
if(ArrayReplace(搜索、替换、引用输入字符串))
{
bool-changed=true;
}
布尔数组替换(字符串[]搜索,字符串[]替换,引用字符串输入字符串)
{
字符串复制=输入字符串;
for(int i=0;i
您不能在一个
字符串中执行此操作。Replace
,您将需要三个不同的调用来调用
字符串。Replace
在C#中可能重复@Habib ok,但是我可以使用
字符串吗。如果
可以,请将
替换为
?比如:
if(string.Replace('a','b')){}
?如果(string.Contains(“x”){//…}
@wzazza,no,C#
需要一个布尔参数或一个导致bool的语句,那么您应该执行
,如果
需要一个布尔参数或一个导致bool的语句,
string.Replace
将返回一个字符串,因此它将失败。
var changed=result!=myString
@MathewFoscarini I最终使用了
字符串。替换(“y”,“1”)。替换(“x”,“2”)…
,然后将更改后的字符串与原始字符串进行比较。
var mappings = new Dictionary<char,char> { {'y', '1' }, { 'x', '2'} , {'z','3'}};

var newChars = inputString
                     .Select(x =>
                     {
                         if (mappings.ContainsKey(x)) 
                                return mappings[x];
                         return x;
                     })
                     .ToArray();

var output = new string(newChars);

bool changed = !(output == inputString);
string inputString = "This is my demoy y string!";
string[] searchFor = new string[] { "y", "x", "z" }; // or char[] if you prefer
string[] replaceWith = new string[] { "1", "2", "3" };
if (ArrayReplace(searchFor, replaceWith, ref inputString))
{
    bool changed = true;
}

bool ArrayReplace(string[] searchFor, string[] replaceWith, ref string inputString) 
{
    string copy = inputString;
    for (int i = 0; i < searchFor.Length && i < replaceWith.Length; i++) 
    {
        inputString = inputString.Replace(searchFor[i], replaceWith[i]);       
    }
    return copy != inputString;
}