将String转换为int的最有效方法? [重复]

问题描述 投票:-1回答:1

我知道有3种方法:

String s = "3";

int x = Integer.parseInt(s);
int x = Integer.valueOf(s);
int x = Integer.decode(s);

由于我经常使用它,最有效的方法是什么? (关于复杂性)

java string int integer
1个回答
4
投票

事实

  • [decode在计算完基数(基数)后调用valueOf => Integer.decode(String s),例如

    • [Integer.decode(String s)八进制基数
    • [0550x15六基]
  • [#598调用valueOf,默认基数为parseInt => 10


解决方案

  • 如果字符串以十进制为基础,请使用Integer.valueOf(String s)

  • 如果您的字符串在其他基础中使用,则为Integer.valueOf(String s)

© www.soinside.com 2019 - 2024. All rights reserved.