Home > Software design >  How to remove " from a string in c# in unity
How to remove " from a string in c# in unity

Time:04-21

I'm trying to make simple programm in Unity or C# I guess, that takes a python list( ["4","2","6","9"] ) in form of a string and convertes is to c# list This is my code:

public List<string> ListMaker(string input)
    {
        input = input.Trim(new char[] {'['});
        input = input.Trim(new char[] {']'});
        input = input.Trim(new char[] {"""});
        List<string> Output = input.Split(',').ToList();
        return Output;
    }

, and I am having trouble getting rid of the " symbol since when I use input.Trim(new char[] {'"'}); it dosen't work like with the [] symbols ( input.Trim(new char[] {']'}); ) so I used the " equivalent (input = input.Trim(new char[] {"""});) and the Console says that it can not convert type string to char. Does anyone have a solution to this problem or am I missing something that is already Online? thanks in advance!

CodePudding user response:

The trim method will only remove the first and last character in your string. I quess you want to remove all " from your string.

foreach (char c in input)
{
    input = input.Replace('\"', '\0');
}

But if you really want the trim method then use this

input = input.Trim(new char[] {'\"'});

CodePudding user response:

you can use Escape Sequences

public List<string> ListMaker(string input)
{
    input = input.Trim(new char[] {'['});
    input = input.Trim(new char[] {']'});
    input = input.Trim(new char[] {'\"'});
    List<string> Output = input.Split(',').ToList();
    return Output;
}

shorter Version would be:

public List<string> ListMaker(string input)
{
    input = input.Trim(new char[] { '[', ']', '\"' });
    List<string> Output = input.Split(',').ToList();
    return Output;
}
  • Related