Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unquote string in C#

Tags:

string

c#

I have a data file in INI file like format that needs to be read by both some C code and some C# code. The C code expects string values to be surrounded in quotes. The C# equivalent code is using some underlying class or something I have no control over, but basically it includes the quotes as part of the output string. I.e. data file contents of

MY_VAL="Hello World!" 

gives me

"Hello World!" 

in my C# string, when I really need it to contain

Hello World! 

How do I conditionally (on having first and last character being a ") remove the quotes and get the string contents that I want.

like image 657
AlastairG Avatar asked Mar 02 '11 15:03

AlastairG


People also ask

How do you add quotation marks in C?

Double quotes (” “), in C programming For printing double quotes(” “), using print() in C we make use of ” \” ” backslash followed by double quote format specifier.

How do you remove quotation marks from a string?

To remove double quotes just from the beginning and end of the String, we can use a more specific regular expression: String result = input. replaceAll("^\"|\"$", ""); After executing this example, occurrences of double quotes at the beginning or at end of the String will be replaced by empty strings.

How do you use quotation marks in a string?

Within a character string, to represent a single quotation mark or apostrophe, use two single quotation marks. (In other words, a single quotation mark is the escape character for a single quotation mark.) A double quotation mark does not need an escape character.

What is quote in C language?

In C and C++ the single quote is used to identify the single character, and double quotes are used for string literals. A string literal “x” is a string, it is containing character 'x' and a null terminator '\0'. So “x” is two-character array in this case.


2 Answers

On your string use Trim with the " as char:

.Trim('"') 
like image 155
TurBas Avatar answered Sep 30 '22 19:09

TurBas


I usually call String.Trim() for that purpose:

string source = "\"Hello World!\""; string unquoted = source.Trim('"'); 
like image 23
Frédéric Hamidi Avatar answered Sep 30 '22 21:09

Frédéric Hamidi