Converting a String with spaces to an Integer in java

前端 未结 5 1780
温柔的废话
温柔的废话 2021-02-19 21:59

I\'m trying to convert a String variable into an integer, only the String looks like this (for example):

String string = \" 12\";

And so the St

5条回答
  •  无人共我
    2021-02-19 22:53

    If you using Java 11, you can use the String::strip method. For example:

    int integer = Integer.parseInt(string.strip());
    

    The strip() method is similar to trim() but uses Character.isWhitespace(int) to resolve spaces. (The Character.isWhitespace(int) method is Unicode-aware.)

提交回复
热议问题