I ‘m working on a Console in which I need to send the command to the exe but I’m stuck as I need to add double quotes before and after command text for sending it to external exe to process.After Struggling 15-20, I found a neat and clean solution for this issue.

How to double-quote a string in C# Code:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {

            string resstr = AddDoubleQuotesInstring("Append double quote to string");
        }
        public static string AddDoubleQuotesInstring(string value)
        {
            //Put a string between double quotes.
            /// <param name="value">Value to be put between double quotes ex: foo</param>
            /// <returns>double quoted string ex: "foo"</returns>
            return "\"" + value + "\"";
        }
    }
}

Let’s I want to save the text file with double string Then how we can achieve that task? Follow the below code.

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class GridView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        WriteFileText();
    }

    public void WriteFileText()
    {
        string spath = System.Web.HttpContext.Current.Server.MapPath("~/FileStorage");
        string path = spath + "/mytext.txt";
        List<Person> _person = new List<Person>();
        _person.Add(new Person { name = "Rahul", lastname = "Sharma", city = "New Delhi", });
        _person.Add(new Person { name = "Tom", lastname = "Curz", city = "Tokyo" });
        char seperator = ';';
        StringBuilder sb = new StringBuilder();
        foreach (Person obj in _person)
        {
            sb.Append("\"" + obj.name + "\"");
            sb.Append(seperator);
            sb.Append("\"" + obj.lastname + "\"");
            sb.Append(seperator);
            sb.Append("\"" + obj.city + "\"");
            sb.AppendLine();
        }
        File.AppendAllText(path, sb.ToString());
    }
}
class Person
{
    public string name { get; set; }
    public string lastname { get; set; }
    public string city { get; set; }
}
Output:

The post C# – How to add double quotes to a string that is inside a variable appeared first on Software Development | Programming Tutorials.



Read More Articles