在我的代码中,我需要将字符串转换为整数 - 字符串来自元数据的提取,然后将其与数字数组及其相应的类型进行比较。不幸的是,该字符串不仅仅包含数字,它包含一些空格和括号,这意味着通常的:
String genre = mf.genre(byteArr);
// this gets the metadata from the byte array (It's of no consequence to this but it makes it easier to understand)
int genreInt = Integer.parseInt(genre);
System.out.println(genreInt);
不起作用,返回
Exception in thread "main" java.lang.NumberFormatException: For input string: " (13)"
由于 13(例如)是我想要/需要的唯一部分,因此从“困惑”字符串中获取这个数字的最简单方法是什么?谢谢
最佳答案
您可以使用以下内容:
System.out.println(genre.replaceAll("[^0-9]", ""));
关于java - 'messy'字符串到int转换java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25228014/