如何删除字符串中的每个第二个字符?

Adr*_*ian 3 .net c# linq performance

如何删除字符串中的每个第二个字符?

例如:

3030313535333635  -> 00155365
3030303336313435  -> 00036145
3032323437353530  -> 02247550
Run Code Online (Sandbox Code Playgroud)

字符串总是16个字符长,结果总是8个字符长 - 被删除的字符总是'3' - 不要问为什么然而 - 我没有想到这个疯狂的源数据.

Rah*_*thi 11

试试这个从字符串中获取其他所有字符: -

 var s = string.Join<char>("", str.Where((ch, index) => (index % 2) != 0));
Run Code Online (Sandbox Code Playgroud)


nee*_*eKo 9

String input = "3030313535333635";
String result = "";
for(int i = 1; i < 16; i +=2 )
{
    result += input[i];
}
Run Code Online (Sandbox Code Playgroud)

  • 你让`StringBuilder`伤心:( (10认同)
  • 没关系,很高兴看到一个很好的老式循环赢得Linq的改变:) (3认同)
  • 我想到了,但后来我想"*嘿,它只有8个字符*";) (2认同)

L.B*_*L.B 6

你可以使用这个着名的类System.Runtime.Remoting.Metadata.W3cXsd2001.SoapHexBinary:)

string str = "3030313535333635";
var hex = System.Runtime.Remoting.Metadata.W3cXsd2001.SoapHexBinary.Parse(str);
var newstr = Encoding.ASCII.GetString(hex.Value);
Run Code Online (Sandbox Code Playgroud)

  • "知名班":) (7认同)