[英]C# - Cannot implicitly convert type `string[]' to `string'
Trying to take a string, split it by spaces, reverse it, and return the array values.尝试获取一个字符串,用空格分割它,反转它,然后返回数组值。
I have looked at other answers that say you can just return the array like this:我看过其他答案说你可以像这样返回数组:
public class Program
{
public static string NameShuffle(string str)
{
string[] strArray = str.Split(' ');
Array.Reverse( strArray );
return strArray;
}
}
But for some reason I am getting an error:但由于某种原因,我收到一个错误:
Cannot implicitly convert type string[] to string
无法将类型 string[] 隐式转换为字符串
Your return type on the method is string
and it should be string[]
该方法的返回类型是
string
,它应该是string[]
public static string[] NameShuffle(string str)
// ^HERE^
{
string[] strArray = str.Split(' ');
Array.Reverse(strArray);
return strArray;
}
if you need to convert it back to a string then do something like this如果您需要将其转换回字符串,请执行以下操作
public static string NameShuffle(string str)
{
string[] strArray = str.Split(' ');
Array.Reverse(strArray);
return string.Join(' ', strArray);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.