且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在java中将字符串的部分转换为int

更新时间:2023-02-03 08:29:01

上面的例子,你可以尝试这样:

Given the example above, you could try something like this:

final int value = Integer.parseInt(input.substring(0, input.indexOf(':'))); //value = 12

其中 input = 12:15 pm

一般来说,只需使用 String#indexOf(String) String#substring(int,int) Integer.parseInt(String)

Generally speaking, just use a combination of String#indexOf(String), String#substring(int, int) and Integer.parseInt(String).