Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add line break after 3 commas

Tags:

c#

linq

I have a string like

"tom: 1, john: 3, timmy: 5, cid: 8, ad: 88, hid: 99, mn: 33"

i want to add line break after 3 commas each

"tom: 1, john: 3, timmy: 5, </br> cid: 8, ad: 88, hid: 99, </br> mn: 33"
like image 929
Ali Hasan Avatar asked Oct 18 '11 09:10

Ali Hasan


People also ask

How to add line break after a specific character JavaScript?

The newline character is \n in JavaScript and many other languages. All you need to do is add \n character whenever you require a line break to add a new line to a string.


5 Answers

I believe for loop would be the most straightforward and clear solution but it is interesting to do it using LINQ:

string input = "tom: 1, john: 3, timmy: 5, cid: 8, ad: 88, hid: 99, mn: 33";
char delimiter = ',';
var allParts = input.Split(delimiter);
string result = allParts.Select((item, index) => (index != 0 && (index+1) % 3 == 0)
                    ? item + delimiter + " </br>"
                    : index != allParts.Count() - 1 ? item + delimiter : item)
                    .Aggregate((i, j) => i + j);

// result (without a comma after the last item)
// "tom: 1, john: 3, timmy: 5, </br> cid: 8, ad: 88, hid: 99, </br> mn: 33"
like image 161
sll Avatar answered Nov 07 '22 16:11

sll


string line ="tom: 1, john: 3, timmy: 5, cid: 8, ad: 88, hid: 99, mn: 33";
Regex regex = new Regex(@"(\w+?:\s+\d+,\s){3}");
string result = regex.Replace(line, "$&<br /> ");
like image 24
BLUEPIXY Avatar answered Nov 07 '22 16:11

BLUEPIXY


Use the following code part, it will work fine.

string input = "tom: 1, john: 3, timmy: 5, cid: 8, ad: 88, hid: 99, mn: 33";
            string[] parts = input.Split(','); 
            StringBuilder result = new StringBuilder();
            int i = 1;
            while(i <= parts.Length) 
            {
                result.Append(parts[i-1] + ","); 
                if (i % 3 == 0)
                {
                    result.Append("<br />");
                }
                i++;
            }

EDIT:

   result = result.Remove(result.ToString().LastIndexOf(','), 1);
   MessageBox.Show(result.ToString());
like image 2
Sai Kalyan Kumar Akshinthala Avatar answered Nov 07 '22 17:11

Sai Kalyan Kumar Akshinthala


The best I can do with a LINQ solution:

    var count = 0;
    input.Aggregate(
        new StringBuilder(),
        (sb, ch) =>
        {
            sb.Append(ch);
            if (ch == ',' && ++count % 3 == 0) sb.Append(" </br>");
            return sb;
        }).ToString();
like image 1
Ilian Avatar answered Nov 07 '22 16:11

Ilian


string splitter = ", ";
string newLine = "<br/>";
int splitAfter = 3;
string s = "tom: 1, john: 3, timmy: 5, cid: 8, ad: 88, hid: 99, mn: 33";
string x = 
   s.Split(new[]{splitter}, StringSplitOptions.None) // Split
    // Make each string entry into a Tuple containing the string itself 
    // and an integer key declaring into which group it falls
    .Select((v, i) => 
       new Tuple<int, string>((int) Math.Floor((double) i/splitAfter), v)) 
    // Group by the key created in the line above
    .GroupBy(kvp => kvp.Item1)
    // Since the key is not needed any more select only the string value
    .Select(g => g.Select(kvp => kvp.Item2) 
    // Join the groups 
    // (in your example each group is a collection of 3 elements)
    .Aggregate((a, b) => a + splitter + b)) 
    // Join all the groups and add a new line in between
    .Aggregate((a, b) => a + splitter + newLine + b); 

That's doing it with "one line" of LINQ. Although I'm not quite sure if that is really desirable considering that it is probably pretty hard for another developer to understand what's happening here at first sight (especially if you don't have much experience with LINQ and especially its GroupBy function).

like image 1
chrischu Avatar answered Nov 07 '22 17:11

chrischu