简体   繁体   English

如何将字符串args [0]转换为char

[英]how to convert string args[0] to char

How do you convert from string to char if, for example, the string is entered as an argument in command line (not using scanner). 例如,如果在命令行中将字符串作为参数输入(不使用扫描仪),则如何从字符串转换为char。 Eg how would you convert "abcd" to char? 例如,如何将"abcd"转换为char?

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();

If you want to convert a String with more than 2 characters to a char array: 如果要将具有两个以上字符的String转换为char数组,请执行以下操作:

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

Otherwise if you only need one specific char, use hello.charAt(0) . 否则,如果只需要一个特定的char,请使用hello.charAt(0)

Please notice that you only can store one single 16bit Unicode character inside a char datatype. 请注意,您只能在char数据类型内存储一个单个16位Unicode字符。 That's the reason why you need an array for more than 2 characters. 这就是为什么您需要一个超过2个字符的数组的原因。

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

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