Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transform a comma-separated string to line-break-separated string?

I have a comma-separated string. How can I convert it into line-break-separated format. My string looks like this:

red,yellow,green,orange,pink,black,white

And needs to be formatted to in this way:

red
yellow
green
orange
pink
black
white

Here is my code:

public static string getcolours()
{
    List<string> colours = new List<string>();
    DBClass db = new DBClass();
    DataTable allcolours = new DataTable();
    allcolours = db.GetTableSP("kt_getcolors");
    for (int i = 0; i < allcolours.Rows.Count; i++)
    {
        string s = allcolours.Rows[i].ItemArray[0].ToString();
        string missingpath = "images/color/" + s + ".jpg";
        if (!FileExists(missingpath))
        {
            colours.Add(s);

        }
    }
    string res = string.Join(", ", colours);

    using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"F:\test.txt", true))
    {

        file.WriteLine(res);
    }
    return res;
}
like image 781
Arun Avatar asked Nov 20 '25 17:11

Arun


1 Answers

res = res.Replace(',','\n');

This should work.

like image 176
TRMI Avatar answered Nov 23 '25 06:11

TRMI



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!