'How to remove " from a string in c# in unity

I'm trying to make a simple program in Unity or C# I guess, that takes a python list( ["4", "2", "6", "9"] ) in form of a string and converts it 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 doesn'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!



Solution 1:[1]

If the string of your Python List looks as follows ["1","2","3","4"] I would reccomend to use the Replace function because trim does only remove trailing and leading characters. Therefore your code may look like:

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

If you have spaces between your commas you may replace them too.

Solution 2:[2]

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;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2