0

Got a string let say

string mystring = "A\nB\nC\nD\nE\nF\nG\n"

want to convert it with | for chunk of 5

string Converted string ="ABCDE|FG"

Any one liner solution..

I am going this way

private void TweakInputLines(string InputData)
{
     List<string> lstInput = new List<string>();
     if (!string.IsNullOrEmpty(InputData))
     {
          lstInput = InputData.Split('\n').ToList();
          if (lstInput.Count > 4)
          {

          }
     }
}
Rahul Chowdhury
  • 1,138
  • 6
  • 29
  • 52

4 Answers4

3

Try this one liner

string mystring = "A\nB\nC\nD\nE\nF\nG\n";
var result = Regex.Replace(mystring.Replace("\n", ""), ".{5}", "$0|");

Here is the demo.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
1

General solution (preserving variable length lines):

string input = "A\nBC\nDEF\nG\nH\nI\nJKL\nMN\nO\nP\nQR\nS";
string output = string.Join("|", input.Split('\n')
                                      .Select((s, i) => new { s, i })
                                      .GroupBy(p => p.i / 5)
                                      .Select(g => string.Join("", g.Select(p => p.s))));

Output:

ABCDEFGH|IJKLMNOP|QRS

Update

If you use .Net 3.5, then you need to add .ToArray() calls in string.Join() methods.

string input = "A\nBC\nDEF\nG\nH\nI\nJKL\nMN\nO\nP\nQR\nS";
string output = string.Join("|", input.Split('\n')
                                      .Select((s, i) => new { s, i })
                                      .GroupBy(p => p.i / 5)
                                      .Select(g => string.Join("", g.Select(p => p.s).ToArray()))
                                      .ToArray());

Update 2

Another option is to use slightly modified solution by @SriramSakthivel

string input = "A\nBC\nDEF\nG\nH\nI\nJKL\nMN\nO\nP\nQR\nS";
string output = Regex.Replace(input, @"((?:.*\n){4}.*)\n", "$1|").Replace("\n", "");
Ulugbek Umirov
  • 12,719
  • 3
  • 23
  • 31
0

Simple code is

private void TweakInputLines(string InputData)
    {
        List<string> lstInput = new List<string>();
        var returnstring = "";
        if (!string.IsNullOrEmpty(InputData))
        {
            lstInput = InputData.Split('\n').ToList();
            if (lstInput.Count > 9999)
            {
                int counter = 0;
                foreach (var eachcharitem in lstInput)
                {
                    counter++;
                    returnstring = returnstring + eachcharitem;
                    if (counter == 5)
                    {
                        returnstring = returnstring + "|";
                        counter = 0;
                    }
                }

            }
        }

    }
rjdmello
  • 865
  • 2
  • 9
  • 14
0

Another solution with the use of Linq - its more or less a "one-liner":

string mystring = "A\nB\nC\nD\nE\nF\nG\n";

var str = mystring
           .Select((value, index) => new { Index = index, Value = value })    // insert Index    
           .GroupBy(i => (int)(i.Index / 5))                                  // group by index / 5
           .Select(value => String.Join("",value.Select(temp => temp.Value))) // create string out of grouped chars
           .Aggregate((a,b) => a + "|" + b);                                  // create one string out of splitted chars 
                                                                              // and join the "|"-string in between
Peter Ittner
  • 551
  • 2
  • 13