Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将字符串值512m转换为整数值512?_Java - Fatal编程技术网

Java 如何将字符串值512m转换为整数值512?

Java 如何将字符串值512m转换为整数值512?,java,Java,如何将字符串值512m转换为整数值512? 你好 你能帮帮我吗?我有一个512m的字符串,我需要转换成整数512 提前谢谢 串s=512m 删除字符串: String str = "512m"; str = str.replace("X", ""); 将字符串转换为int int foo = Integer.parseInt("1234"); 您可以使用正则表达式来提取正则表达式组中的数字字符。然后将提取的字符转换为整数 str.replace("\\D", ""); 将从字符串中删除所

如何将字符串值512m转换为整数值512? 你好 你能帮帮我吗?我有一个512m的字符串,我需要转换成整数512

提前谢谢
串s=512m

删除字符串:

String str = "512m";
str = str.replace("X", "");
将字符串转换为int

int foo = Integer.parseInt("1234");

您可以使用正则表达式来提取正则表达式组中的数字字符。然后将提取的字符转换为整数

str.replace("\\D", ""); 
将从字符串中删除所有非数字字符,然后您可以对其进行解析:

Integer.parseInt(str);

将所有非数字替换为空,并将其解析为整数:

    String str = "512m";
    int i = Integer.parseInt(str.replaceAll("\\D", ""));    

    System.out.println(i);

我的意思是字符串s=512m;这个问题似乎离题了,因为它是关于homework@zvdh当前位置家庭作业不是离题。但是,如果完全没有做出任何努力,则表示反对。Integer.parseIntvalue.substring0,value.length-1;谢谢,我在这方面做得很好way@T.J.Crowder,以前其中一个标志太具体了,没有用处或类似的东西。现在我找不到这个标志,你能建议我选择什么作为关闭原因吗?我认为上面描述的问题对于作者来说是特定的,对于其他SO用户来说不会有用。