2013-04-11 89 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個字符的數組。

相關問題