Convert Char to string in C#

Asked

Viewed 1,550 times

3

I’m trying to convert a char into string to use in split(), but it’s no use

string path = HttpContext.Current.Request.Url.AbsolutePath;
path = path.ToString();
string[] caminho = path.Split("/");

I get the following error Argumento 1:Não é possivel converter string para char Is there any way to daze this ? this using C# MVC Asp.net

1 answer

4


You are using double quotes, which is the literal of string. That one Overload of the method Split expecting a char. Use simple quotes this way:

string[] caminho = path.Split('/');

Of documentation:

Char constants can be written as literals of characters, hexadecimal escape sequence or as representation Unicode. You can also convert the character codes integral. In the following example, four char variables are initialized with the same character X:

char[] chars = new char[4];

chars[0] = 'X';        // Character literal
chars[1] = '\x0058';   // Hexadecimal
chars[2] = (char)88;   // Cast from integral type
chars[3] = '\u0058';   // Unicode

foreach (char c in chars)
{
    Console.Write(c + " ");
}
// Output: X X X X
  • Tochararray wouldn’t do either? string[] caminho = path.Split("/".ToCharArray());

  • 2

    @Pablotondolodevargas and vnbrs: the Split has override for char[] also, but it wouldn’t make any sense to use it when you can just change the quotes. The answer gives the best way for sure.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.