在java中将字符串的部分转换为int [英] converts parts of a string to int in java

查看:183
本文介绍了在java中将字符串的部分转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在字符串中取一些数字,并将它们转换为整数类型?例如,如果用户输入12:15 pm,我如何获得1和2,并创建一个值为12的int。

I was wondering how can I take some numbers in a string and convert them to an integer type? for example if a user entered 12:15pm how can I get 1 and 2 and make an int with value 12?

推荐答案

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

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).

这篇关于在java中将字符串的部分转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆