How to trim quote(") mark from string in c#

Hakan
Hakan used Ask the Experts™
on
I have a strings like below;

"PR"_"CM_" like that i'd like to trim all " signs from string.

Any help would be grateful.

Thank you.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018
Commented:
Hi Hakan,

Please use

string.Replace("\"", string.Empty);

Regards,
Chinmay.
Most Valuable Expert 2015
Distinguished Expert 2018
Commented:
It's the variable that takes the method:

string input = "\"PR\"_\"CM_\"";
string trimmedInput = input.Replace("\"", string.Empty);

Console.WriteLine(input);
Console.WriteLine(trimmedInput);

Open in new window

Output:

"PR"_"CM_"
PR_CM_

Open in new window

ste5anSenior Developer

Commented:
Well, do you want to replace it or trim it? Trimming means to get the left and right side of the string corrected, but not the inner parts. Also unescaping could be possible.

E.g.

namespace ConsoleCS
{
    using System;

    public class Program
    {
        public static void Main(string[] args)
        {
            const string sample = "\"PR\"\"_\"\"CM_\"";
            Console.WriteLine($"Sample:\t\t{sample}");

            string replaced = sample.Replace("\"", "");
            Console.WriteLine($"Replace:\t{replaced}");

            string trimmed = sample;
            while (trimmed.Substring(0, 1) == "\"") { trimmed = trimmed.Substring(1); }
            while (trimmed.Substring(trimmed.Length - 1, 1) == "\"") { trimmed = trimmed.Substring(0, trimmed.Length - 1); }
            Console.WriteLine($"Trim:\t\t{trimmed}");

            string unescaped = sample.Replace("\"\"", "\"");
            unescaped = unescaped.Substring(1, unescaped.Length - 2);
            Console.WriteLine($"Unescape:\t{unescaped}");

            Console.WriteLine("\nDone.");
            Console.ReadLine();
        }
    }
}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial