我正在尝试从String数组元素中解析一个int.这是我的代码:
- String length = messageContents[k].replace("Content-Length:","").replace(" ","");
- System.out.println("Length is: " + length);
- int test= Integer.parseInt(length);
System.out.println返回以下内容:Length为:23
但是,当我尝试将String解析为int时,抛出java.lang.NumberFormatException;
- java.lang.NumberFormatException: For input string: "23"
我有点困惑23如何解析成int.我只能假设其中有一些其他角色正在阻止它,但我不能在我的生命中看到它.
有什么建议?
谢谢
更新
- Despite the String length having only two characters,Java reports its length as three:
- Length is: '23'
- Length of length variable is: 3
- length.getBytes = [B@126804e
解决方法
试试这个变种:
- int test= Integer.parseInt(length.trim());