2013-04-11 50 views
0

如果在命令行(不使用扫描程序)中输入字符串作为参数,如何将字符串转换为字符。例如。你会如何将"abcd"转换为char?如何将字符串args [0]转换为字符

String input = args[0] 
String [] part = input.split(""); 

//splits string into 2 parts (action and characters to encode) 
String action = part[0]; 
// action is what is done to letter i.e. decrypt or encrypt 
String plainText = part[1]; 
char [] letters = plainText.toCharArray(); 
+4

这难道不是你刚才问的那个问题吗? http://stackoverflow.com/questions/15942303/convert-string-from-args0-into-char-then-populate-and-print-a-2d-array-with-th – maba 2013-04-11 07:53:35

+1

是的,你问同样的事情1小时前。多次发布相同的问题不会帮助您找到答案。 – BackSlash 2013-04-11 07:56:35

回答

0

如果你想要一个String超过2个字符转换为char阵列:

String hello = "Hello"; 
char[] char_array = hello.toCharArray(); 

否则,如果你只需要一个特定的字符,使用hello.charAt(0)

请注意,您只能在char数据类型中存储一个单一的16位Unicode字符。这就是为什么你需要一个超过2个字符的数组。