需要将此 php 代码转换为 C#
strtr($input, '+/', '-_')
是否存在等效的 C# 函数?
@Damith @Rahul Nikate @Willem van Rumpt
您的解决方案通常有效。有一些特殊情况会产生不同的结果:
echo strtr("hi all, I said hello","ah","ha");
returns
ai hll, I shid aello
而你的代码:
ai all, I said aello
我认为 phpstrtr
同时替换输入数组中的字符,而您的解决方案执行替换后,结果将用于执行另一替换。
所以我做了以下修改:
private string MyStrTr(string source, string frm, string to)
{
char[] input = source.ToCharArray();
bool[] replaced = new bool[input.Length];
for (int j = 0; j < input.Length; j++)
replaced[j] = false;
for (int i = 0; i < frm.Length; i++)
{
for(int j = 0; j<input.Length;j++)
if (replaced[j] == false && input[j]==frm[i])
{
input[j] = to[i];
replaced[j] = true;
}
}
return new string(input);
}
所以代码
MyStrTr("hi all, I said hello", "ah", "ha");
报告与 php 相同的结果:
ai hll, I shid aello
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)