locked
Adding double quotes to text RRS feed

  • Question

  • User-73514677 posted

    Hi.

    I am reading the contents of dataset and passing it to a textfile. I have used the below code, which works.

    foreach (DataRow row in ds.Tables[0].Rows)
                            {
                                object[] array = row.ItemArray;
                                for (int i = 0; i < array.Length - 1; i++)
                                {
                                    sw.Write(array[i].ToString() + "~");
                                }
                                sw.WriteLine(array[array.Length - 1].ToString());
                            }

    If the datarow contins text/special characters, i need to put the data in double quotes, if the datarow contains only numbers, then double quotes are not required.

    How to achive this?

    Thanks

    Wednesday, November 25, 2020 2:42 PM

Answers

  • User1535942433 posted

    Hi venkatzeus,

    As far as I think,you could convert.ToString if the  data is text/special characters.

    Just like this:

    if(example is string)
    {
    string x = (Convert.ToString("\"") + example) + "\"";
    }

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 26, 2020 6:21 AM

All replies

  • User303363814 posted

    You can check if a string is a number with TryParse.  You could make a little method

    bool isNumber(string inp) {
       long dummy;
       return long.TryParse(inp, out dummy);
    }

    Wednesday, November 25, 2020 9:10 PM
  • User1535942433 posted

    Hi venkatzeus,

    As far as I think,you could convert.ToString if the  data is text/special characters.

    Just like this:

    if(example is string)
    {
    string x = (Convert.ToString("\"") + example) + "\"";
    }

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 26, 2020 6:21 AM
  • User-821857111 posted

    You can check if a string is a number with TryParse.  You could make a little method

    bool isNumber(string inp) {
       long dummy;
       return long.TryParse(inp, out dummy);
    }

    You can reduce that using a discard and an expression bodied member:

    bool isNumber(string inp) => long.TryParse(inp, out _);

    Thursday, November 26, 2020 6:44 AM