How to delete every 2nd character in a string?

前端 未结 5 2029
执念已碎
执念已碎 2020-12-21 05:48

How to delete every 2nd character in a string?

For example:

3030313535333635  -> 00155365
3030303336313435  -> 00036145
3032323437353530  ->         


        
相关标签:
5条回答
  • 2020-12-21 06:16

    You can use this well-known class 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);
    
    0 讨论(0)
  • 2020-12-21 06:22

    Code in Java Language

    String input= "3030313535333635"
    String output="";
    
    for(int i=1;i<input.length();i=i+2)
    {
        output+=input.charAt(i).toString();
    }
    System.out.println(output);
    
    0 讨论(0)
  • 2020-12-21 06:32

    Try this to get the every other character from the string:-

     var s = string.Join<char>("", str.Where((ch, index) => (index % 2) != 0));
    
    0 讨论(0)
  • 2020-12-21 06:33
    String input = "3030313535333635";
    String result = "";
    for(int i = 1; i < 16; i +=2 )
    {
        result += input[i];
    }
    
    0 讨论(0)
  • 2020-12-21 06:37

    Using a StringBuilder to create a string will save resources

    string input = "3030313535333635";
    var sb = new StringBuilder(8); // Specify capacity = 8
    for (int i = 1; i < 16; i += 2) {
        sb.Append(input[i]);
    }
    string result = sb.ToString();
    
    0 讨论(0)
提交回复
热议问题