当我尝试乘以charAt时,我收到了"大号":
String s = "25999993654";
System.out.println(s.charAt(0)+s.charAt(1));
Run Code Online (Sandbox Code Playgroud)
结果:103
但是当我想只收到一个号码时,没关系.
在JAVA文档中:
the character at the specified index of this string. The first character is at index 0.
Run Code Online (Sandbox Code Playgroud)
所以我需要解释或解决方案(我认为我应该将字符串转换为int,但在我看来这是不必要的工作)
T.J*_*der 12
char是一个完整的类型.s.charAt(0)示例中的值是char数字50 的版本(字符代码'2').s.charAt(1)是(char)53.当你使用+它们时,它们会转换为整数,最终会得到103(而不是100).
如果你想使用数字 2和5,是的,你必须解析.或者如果你知道它们是标准的ASCII样式数字(字符代码48到57,包括在内),你可以从它们中减去48(因为48是字符代码'0').或者更好的是,正如Peter Lawrey在其他地方指出的那样,使用Character.getNumericValue它可以处理更广泛的角色.
| 归档时间: |
|
| 查看次数: |
3194 次 |
| 最近记录: |