ayman
ayman

Reputation: 58

How to remove double quotes inside a variable?

Let's say we have variable x with value "hello world" (with quotes)

string x = @"""hello world""";

string y = ???

How to convert x to hello world (without quotes) and assign it to y ?

Upvotes: 2

Views: 1990

Answers (4)

Mokky Miah
Mokky Miah

Reputation: 1353

use regex to remove all quotes or any characters you wish. eg s/\"//g

UPDATE:

#!/usr/bin/perl -w
use strict;
use warnings;

my $x = '"""" Hello world """"';
my $y = $x;

$y =~ s/\"//g;

print $y;

Upvotes: -1

Racil Hilan
Racil Hilan

Reputation: 25371

You want to remove the quotes not escape them. I corrected the title of your question to reflect that.

If you only want to remove the quotes from the beginning and end of the value, use:

string y = x.Trim('"');

If you want to remove all quotes wherever they appear in the value, use:

string y = x.Replace( "\"", String.Empty );

This answer is the same as the same as the ones by Habib and Craig W., but I just grouped them together with the appropriate explanation.

Upvotes: 1

Craig W.
Craig W.

Reputation: 18175

Replace the double quotes with an empty string.

y = x.Replace( "\"", String.Empty );

Upvotes: 3

Habib
Habib

Reputation: 223412

You can use string.Trim passing it a double quote. It would remove double quote from start and end of the string. Like.

string y = x.Trim('"');

Upvotes: 7

Related Questions