Converting a String with spaces to an Integer in java

前端 未结 5 1781
温柔的废话
温柔的废话 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:47

    Would .trim work? And if so, how would I use that?

    Yes, trim() will work, it will remove leading and trailing spaces from String,

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

提交回复
热议问题