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
- Write a value which contain comma to a CSV file in c#?
- Reading CSV File with cells containing commas c#
- Split CSV with columns may contain ‘,’ Comma C#
- [Simple Way]-Cascading DropDownList in Asp.Net Mvc Using Jquery Ajax
- [Simple Way]-How to get data from database using JQuery Ajax in asp net MVC
- [Simple Way]-ASP.NET Core Upload Multiple File Web API model
- [Simple Way]- Image Upload in .NET Core Web API
- [Easy Way]-Receive File and other form data together in ASP.NET Core Web API
- Replace image in word document using C#
- How to add new rows to an existing word document table in C#
- How to add double quotes to the xml tag attributes that is a string content
- how to add double quotes to a string which contains comma?
- How can I add the string text to the method that get string[]?
- How to deal with single or double quote in string variable in Access SQL expression
- string escape double quotes inside single quotes
- How to Bind Embedded Image with variable ImageSource that is specified by a string in C# code behind?
- how separate a string and a double values inside a square bracket using regex
- How to get a specific string between a string that are inside another string?
- How to add filtered row From Table inside Dataset that already get 3 other Tables?
- How can I check if my string starts with <p> and add that text if it doesn't in C#
- How to get character positions and assign that vale to variable from string using C#?
- How to insert string containing single or double quotes
- How to add string that includes '=' into stringlist?
- How to fix construction to add a bool value in a string vector
- How do you dynamically (via code) add custom Controls that were made in XAML in WPF?
- How do I add a string array within a string array array in a string list?
- How to call control on a Panel that is inside usercontrol
- How do I get a non lowercase string after quotes in the titlecase condition
- How to handle double numbers that exceeds double.Max in C#?
- How to deserialize WebAPI string response which has doubles quotes content for some column
- How do I add 1 to a datarow that is being cast as an object
- C# How to make a foreach loop not override a string variable
- How to add new element to existing 2d string array?
- How can I add an onclick event to a div that was created in the C# code behind?
- how do I use a connecting string variable defined in a file reuse in another file in same C# project?
- How to find a string element in one of several arraylists inside a list?? C#
- VSPackage, how to add a pointer type variable to a CodeClass
- Parse and find string in between (English string inside double square bracket) with C#?
- How to use one string variable with multiple Conditional Or operators
- How do I add a string from c# function into a meta tag (inside quotes)?
- Can't see any test results in VSTS build dashboard
- blazor SignInAsync have This Error 'Headers are read-only, response has already started.'
- Why isn't the Cache invalidated after table update using the SqlCacheDependency?
- Execute action immediately
- Cannot run a Javascript code from Codebehind in an Aspx page
- C# code to start selenium-server on remote machine
- integer lattice pairs within a circle c#
- C# function for converting string to int with default value parameter
- WPF WriteableBitmap drawing over loaded picture with mouse and save. Strange cursor shift
- Serializing Dictionary<string,string> to array of "name": "value"
- Add attribute on property of a runtime created type using reflection
- How to call unmanaged dll to populate struct in C# using a pointer
- Strings.resx cannot be accessed?
- MVC3 strong typed model with multiple inputs
- How to set maxlength of column name while export to dbf file
- Why user command and command handler?
- Accessing a dynamic RadioButtonList array object
- C# program keeps asking user input questions more than once
- c# Regex for file extension embedded in a string
- Create PDF from HTML with vertical text