c#中字符串的反转部分
reverse part of string in c#
如何反转用户输入字符串的一部分?
只有数字不应该反转所有其他部分必须反转。
ABC123DEF --> CBA123FED
DISK0123CAR --> KSID0123RAC
596ABCDEF --> 596FEDCBA
提前致谢
这是我的代码:
public static string ReverseStr(string sStrRev)
{
string output = "";
Dictionary<int, char> SChar = new Dictionary<int, char>();
int Cposition = 0;
for (int i = sStrRev.Length - 1; i >= 0; i--)
{
if (sStrRev[i] != '1' && sStrRev[i] != '2' && sStrRev[i] != '3'
&& sStrRev[i] != '4' && sStrRev[i] != '5'
&& sStrRev[i] != '6' && sStrRev[i] != '7'
&& sStrRev[i] != '8' && sStrRev[i] != '9'
&& sStrRev[i] != '0')
output += sStrRev[i];
else
{
SChar.Add(Cposition, sStrRev[i]);
}
Cposition++;
}
for (int i = 0;i<sStrRev.Length ; i++)
{
if (SChar.ContainsKey(i))
output.Insert(i, SChar[i].ToString());
}
return output;
}
我建议使用正则表达式来匹配所有要反转的部分,并使用Linq来反转它们:
using System.Linq;
using System.Text.RegularExpressions;
...
string source = "DISK0123CAR";
// KSID0123RAC
string result = Regex.Replace(source,
"[^0-9]+", // all except numbers ...
match => string.Concat(match.Value.Reverse())); // ... should be reversed
如果你想把它包装成一个方法:
public static string ReverseStr(string sStrRev) {
// when implementing public methods do not forget to validate the input
if (string.IsNullOrEmpty(sStrRev))
return sStrRev;
return Regex.Replace(sStrRev,
"[^0-9]+",
match => string.Concat(match.Value.Reverse()));
}
编辑:请注意解决方案不交换块:
ABC123DEF --> CBA123FED // <- CBA and FED are not swapped
DISK0123CAR --> KSID0123RAC
596ABCDEF --> 596FEDCBA
以防你也想颠倒区块的顺序
string result = string.Concat(Regex
.Split(source, "([^0-9]+)")
.Reverse()
.Select(chunk => chunk.All(c => c >= '0' && c <= '9')
? chunk
: string.Concat(chunk.Reverse())));
结果会是
ABC123DEF --> FED123CBA // chunks are swapped
DISK0123CAR --> RAC0123KSID
596ABCDEF --> FEDCBA596
如何反转用户输入字符串的一部分? 只有数字不应该反转所有其他部分必须反转。
ABC123DEF --> CBA123FED
DISK0123CAR --> KSID0123RAC
596ABCDEF --> 596FEDCBA
提前致谢 这是我的代码:
public static string ReverseStr(string sStrRev)
{
string output = "";
Dictionary<int, char> SChar = new Dictionary<int, char>();
int Cposition = 0;
for (int i = sStrRev.Length - 1; i >= 0; i--)
{
if (sStrRev[i] != '1' && sStrRev[i] != '2' && sStrRev[i] != '3'
&& sStrRev[i] != '4' && sStrRev[i] != '5'
&& sStrRev[i] != '6' && sStrRev[i] != '7'
&& sStrRev[i] != '8' && sStrRev[i] != '9'
&& sStrRev[i] != '0')
output += sStrRev[i];
else
{
SChar.Add(Cposition, sStrRev[i]);
}
Cposition++;
}
for (int i = 0;i<sStrRev.Length ; i++)
{
if (SChar.ContainsKey(i))
output.Insert(i, SChar[i].ToString());
}
return output;
}
我建议使用正则表达式来匹配所有要反转的部分,并使用Linq来反转它们:
using System.Linq;
using System.Text.RegularExpressions;
...
string source = "DISK0123CAR";
// KSID0123RAC
string result = Regex.Replace(source,
"[^0-9]+", // all except numbers ...
match => string.Concat(match.Value.Reverse())); // ... should be reversed
如果你想把它包装成一个方法:
public static string ReverseStr(string sStrRev) {
// when implementing public methods do not forget to validate the input
if (string.IsNullOrEmpty(sStrRev))
return sStrRev;
return Regex.Replace(sStrRev,
"[^0-9]+",
match => string.Concat(match.Value.Reverse()));
}
编辑:请注意解决方案不交换块:
ABC123DEF --> CBA123FED // <- CBA and FED are not swapped
DISK0123CAR --> KSID0123RAC
596ABCDEF --> 596FEDCBA
以防你也想颠倒区块的顺序
string result = string.Concat(Regex
.Split(source, "([^0-9]+)")
.Reverse()
.Select(chunk => chunk.All(c => c >= '0' && c <= '9')
? chunk
: string.Concat(chunk.Reverse())));
结果会是
ABC123DEF --> FED123CBA // chunks are swapped
DISK0123CAR --> RAC0123KSID
596ABCDEF --> FEDCBA596