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"
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.
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"
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 /> ");
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());
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();
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With