0

Why am I getting this error?

public class ParameterParser
{
    public List<string> ParseParameter(string queryString)
    {
        queryString = queryString.Replace(" ", "");

        string[] strs = queryString.Split(@"(", @"=", @",", @"<>", 
                                          StringSplitOptions.None);

        List<string> parameters = new List<string>();

        foreach (string ss in strs)
        {
            string s = ss.Trim(')');

            if (s.StartsWith("@") && !s.Equals("") && s!=null)
            {
                parameters.Add(s.Replace(" ", ""));
            }
        }

        return parameters;
    }
}

Error 3 The best overloaded method match for 'string.Split(params char[])' has some invalid arguments F:...\ParameterParser.cs

2 Answers 2

6

If you want to pass an array of strings, you have to do that explicitly:

string[] strs = queryString.Split(new string[] {"(", "=", ",", "<>"}, 
                                  StringSplitOptions.None);

I suspect you were modelling your code on something like this:

string[] strs = queryString.Split('(', '=', ',');

This is using a parameter array (the params modifier in C#). Parameter arrays are only applicable for the final parameter, and no overload of String.Split takes a params string[]. That's why it wasn't working for you.

Note that I've changed the strings into simple string literals - I would recommend only using verbatim string literals when you actually need to.

Sign up to request clarification or add additional context in comments.

Comments

0

@"(", @"=", @",", @"<>" is not a single string or a char[] array

Try this:

string[] strs = queryString.Split(new string[] {@"(", @"=",@",",@"<>" },StringSplitOptions.None);

2 Comments

I didn't say that it did I just said it wasn't! ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.