简体   繁体   English

如何将字符串转换为ushort

[英]how to convert a string to a ushort

I have researched all of the questions that match my query and tried their solutions however none appear to work for me so I am asking how I convert a string to a ushort I have this set up all ready 我已经研究了所有与查询匹配的问题,并尝试了解决方案,但是似乎没有一个适合我,因此我问如何将字符串转换为ushort我已经准备好了

public static string vid
{
    get { return "<vehicleid>"; }
}

I've tried to use this : short[] result = vid.Select(c => (ushort)c).ToArray(); 我尝试使用此方法: short[] result = vid.Select(c => (ushort)c).ToArray(); but when I go to put the vid ushort into this bit 但是当我去把vid ushort放入这部分时

[RocketCommand("vehicle", "This command spawns you a vehicle!", "<vehicleid>", AllowedCaller.Player)]
public void ExecuteCommandVehicle(IRocketPlayer caller, string[] command)
{
    UnturnedPlayer spawner = (UnturnedPlayer)caller;
    spawner.GiveVehicle(vid);
}

I get the following error : 我收到以下错误:

Severity Code Description Project File Line Suppression State Error CS1503 Argument 1: cannot convert from 'string' to 'ushort' arena v by FridgeBlaster C:\\Users\\Jonathan\\Documents\\Visual Studio 2015\\Projects\\arena v by FridgeBlaster\\arena v by FridgeBlaster\\vehicle.cs 71 Active 严重性代码说明项目文件行抑制状态错误CS1503参数1:无法从FridgeBlaster将'string'转换为'ushort'区域v由FridgeBlaster C:\\ Users \\ Jonathan \\ Documents \\ Visual Studio 2015 \\ Projects \\ arena v由FridgeBlaster \\ arena v由FridgeBlaster \\ vehicle.cs 71有效

What you're looking for is ushort.TryParse or ushort.Parse methods. 您正在寻找的是ushort.TryParseushort.Parse方法。
I would suggest using this piece of code : 我建议使用这段代码:

ushort[] result = vid.Where(i => ushort.TryParse(i, out short s)).Select(ushort.Parse);

Or if you do not use latest C# version : 或者,如果您不使用最新的C#版本:

ushort[] result = vid.Where(i => { ushort r = 0; return ushort.TryParse(i, out r); }).Select(ushort.Parse);

Okay so the problem ( as what your error says ) is that your GiveVehicle method accepts ushort as an argument and you're putting string type in it. 好的,问题是(如您的错误所说)是您的GiveVehicle方法接受ushort作为参数,并且您要在其中string类型。 Try doing something like this : 尝试做这样的事情:

ushort val = 0;
if(ushort.TryParse(vid, out val))
{
    UnturnedPlayer spawner = (UnturnedPlayer)caller;
    spawner.GiveVehicle(val);
}

Based on this source which is responsible for calling method marked with RocketCommand attribute. 基于此源 ,它负责调用标有RocketCommand属性的方法。 Your <"vehicleid"> is/should be stored in a command array on first place. 您的<"vehicleid">首先应该存储在command数组中。 So to get this out and convert use something like this : 因此,要进行转换并使用以下代码:

if(command.Length > 0)
{
    ushort val = 0;
    if(!string.IsNullOrWhiteSpace(command[0]) && ushort.TryParse(command[0], out val))
    {
        UnturnedPlayer spawner = (UnturnedPlayer)caller;
        spawner.GiveVehicle(val);
    }
}

对于简单的字符串到ushort的转换: UInt16.Parse(string)

Either you want to convert the string to a short: 您想将字符串转换为简短形式:

Int16.Parse(...)

or 要么

Int16.TryParse(...)

or you want to convert char to short by first converting string to array of chars: 或者您想通过先将字符串转换为char数组将char转换为short:

vid.ToCharArray[..]

Based on the line short[] result = vid.Select(c => (ushort)c).ToArray(); 基于该行short[] result = vid.Select(c => (ushort)c).ToArray(); it looks like there might be multiple IDs in a single string (each character, I assume). 看起来一个字符串中可能有多个ID(我假设每个字符)。

If that's the case, try something like this: 如果是这种情况,请尝试以下操作:

ushort[] result = vid.ToCharArray().Select(c => (ushort) c).ToArray();

If you've got several actual numbers in the string separated by a character (eg "13,15,18"), give this a shot: 如果字符串中有几个实际数字,并且用字符分隔(例如“ 13,15,18”),请尝试一下:

ushort[] result = vid.Split(separator).Select(str => ushort.Parse(str)).ToArray();

For either of these options, make sure you include using directives for the System and System.Linq namespaces. 对于这两个选项,请确保包括对SystemSystem.Linq命名空间使用using指令。


With discussion on this and other answers, it looks like command is the result of tokenizing "/vehicle <id>" . 通过讨论此答案和其他答案,看来command是对"/vehicle <id>"进行标记化的结果。 In this case, the body of the ExecuteCommandVehicle(IRocketPlayer, string[]) method should be similar to this: 在这种情况下, ExecuteCommandVehicle(IRocketPlayer, string[])方法的主体应与此类似:

ushort result = 0;
if (ushort.TryParse(command[1], out result)) {
  UnturnedPlayer spawner = caller as UnturnedPlayer;
  caller?.GiveVehicle(result);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM