命令行参数 [英] command line arguments

查看:132
本文介绍了命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将命令行参数转换为char类型



我正在写这个

how to convert command line arguments to char type

I am writing this

string[] ar=Environment.GetCommandLineArgs();
char c =Convert.ToChar(a);





正在以这种方式转换正确吗?



is converting in this way correct?

推荐答案

没有。命令行参数是字符串,它们是(有效)字符数组。您不能将字符串转换为字符,只能将书架转换为单个小说。你的代码甚至都没有尝试 - 它甚至不能自己使用参数!



取决于你想用命令行参数做什么,您可以直接将其作为字符串进行比较:

No. The command line arguments are strings, and they are (effectively) arrays of characters. You cannot convert a string to a character any more than you can convert a shelf of books into a single novel. And your code doesn''t even try - it deoesn''t even use the arguments themselves!

Depending on what you want to do with the command line argument, you could compare it directly as a string:
foreach(string cmd in Environment.GetCommandLineArgs())
   {
   switch(cmd)
      {
      case "Command1": ProcessCommand1(); break;
      case "Command2": ProcessCommand2(); break;
      ...
      }
   }


当GetCommandLineArgs方法返回一个字符串数组时,它的任何元素都是串。 字符串 [ ^ ]不是字符而不是字符数组,如C中所示。您无法转换字符串到一个char,因为它包含许多字符。但是,您可以从中提取任何字符(使用索引器或子字符串 [ ^ ]方法),或将其转换为字符数组 [ ^ ]。

所以这取决于你究竟想要什么...



但是对于解析命令行参数,你最好使用第三方库,如下所示: http://commandline.codeplex.com/ [ ^ ]
As the GetCommandLineArgs method return a string array, any element of it is a string. A string[^] is not a character and not a character array, as in C. You can not convert a string to a single char, since it contains many characters. You can however extract any character from it (using indexer or substring[^] method), or convert it to a character array[^].
So it depends on what yo want exactly...

But for parsing command line arguments, you better use a third party library, like this one: http://commandline.codeplex.com/[^]


Convert.ToChar(string) [ ^ 仅转换为t他是第一个字符串的字符。

您可以使用 string.ToCharArray [ ^ ]转换所有字符在字符串到char数组中例如:

Convert.ToChar(string)[^] coverts only the first char of the string.
You can use string.ToCharArray[^] to convert all the char in a string to char array eg:
string delimStr = " ,.:";
char [] delimiter = delimStr.ToCharArray();



所以你做出决定你的参数如何传递以及如何使用这些方法来解析你的参数。


so you make a decision on how your arguments are passed and how to use these methods to parse your arguments.


这篇关于命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆