Add separator to string at every N characters?

后端 未结 13 2171
半阙折子戏
半阙折子戏 2020-11-27 13:20

I have a string which contains binary digits. How to separate string after each 8 digit?

Suppose the string is:

string x = \"111111110000000011111111         


        
相关标签:
13条回答
  • 2020-11-27 13:32

    Ugly but less garbage:

    private string InsertStrings(string s, int insertEvery, char insert)
    {
        char[] ins = s.ToCharArray();
        int length = s.Length + (s.Length / insertEvery);
        if (ins.Length % insertEvery == 0)
        {
            length--;
        }
        var outs = new char[length];
        long di = 0;
        long si = 0;
        while (si < s.Length - insertEvery)
        {
            Array.Copy(ins, si, outs, di, insertEvery);
            si += insertEvery;
            di += insertEvery;
            outs[di] = insert;
            di ++;
        }
        Array.Copy(ins, si, outs, di, ins.Length - si);
        return new string(outs);
    }
    

    String overload:

    private string InsertStrings(string s, int insertEvery, string insert)
    {
        char[] ins = s.ToCharArray();
        char[] inserts = insert.ToCharArray();
        int insertLength = inserts.Length;
        int length = s.Length + (s.Length / insertEvery) * insert.Length;
        if (ins.Length % insertEvery == 0)
        {
            length -= insert.Length;
        }
        var outs = new char[length];
        long di = 0;
        long si = 0;
        while (si < s.Length - insertEvery)
        {
            Array.Copy(ins, si, outs, di, insertEvery);
            si += insertEvery;
            di += insertEvery;
            Array.Copy(inserts, 0, outs, di, insertLength);
            di += insertLength;
        }
        Array.Copy(ins, si, outs, di, ins.Length - si);
        return new string(outs);
    }
    
    0 讨论(0)
  • 2020-11-27 13:32

    One way using LINQ:

    string data = "111111110000000011111111000000001111111100000000";
    const int separateOnLength = 8;
    
    string separated = new string(
        data.Select((x,i) => i > 0 && i % separateOnLength == 0 ? new [] { ',', x } : new [] { x })
            .SelectMany(x => x)
            .ToArray()
        );
    
    0 讨论(0)
  • 2020-11-27 13:34

    A little late to the party, but here's a simplified LINQ expression to break an input string x into groups of n separated by another string sep:

    string sep = ",";
    int n = 8;
    string result = String.Join(sep, x.InSetsOf(n).Select(g => new String(g.ToArray())));
    

    A quick rundown of what's happening here:

    • x is being treated as an IEnumberable<char>, which is where the InSetsOf extension method comes in.
    • InSetsOf(n) groups characters into an IEnumerable of IEnumerable -- each entry in the outer grouping contains an inner group of n characters.
    • Inside the Select method, each group of n characters is turned back into a string by using the String() constructor that takes an array of chars.
    • The result of Select is now an IEnumerable<string>, which is passed into String.Join to interleave the sep string, just like any other example.
    0 讨论(0)
  • 2020-11-27 13:39

    If I understand your last requirement correctly (it's not clear to me if you need the intermediate comma-delimited string or not), you could do this:

    var enumerable = "111111110000000011111111000000001111111100000000".Batch(8).Reverse();
    

    By utilizing morelinq.

    0 讨论(0)
  • 2020-11-27 13:40

    I am more than late with my answer but you can use this one:

        static string PutLineBreak(string str, int split)
        {
            for (int a = 1; a <= str.Length; a++)
            {
                if (a % split == 0)
                    str = str.Insert(a, "\n");
            }
    
            return str;
        }
    
    0 讨论(0)
  • 2020-11-27 13:42

    I did it using Pattern & Matcher as following way:

    fun addAnyCharacter(input: String, insertion: String, interval: Int): String {
      val pattern = Pattern.compile("(.{$interval})", Pattern.DOTALL)
      val matcher = pattern.matcher(input)
      return matcher.replaceAll("$1$insertion")
    }
    

    Where:

    input indicates Input string. Check results section.

    insertion indicates Insert string between those characters. For example comma (,), start(*), hash(#).

    interval indicates at which interval you want to add insertion character.

    input indicates Input string. Check results section. Check results section; here I've added insertion at every 4th character.

    Results:

    I/P: 1234XXXXXXXX5678 O/P: 1234 XXXX XXXX 5678

    I/P: 1234567812345678 O/P: 1234 5678 1234 5678

    I/P: ABCDEFGHIJKLMNOP O/P: ABCD EFGH IJKL MNOP

    Hope this helps.

    0 讨论(0)
提交回复
热议问题