获取字符串“600sp”的整数部分的最佳方法? [英] Best way to get integer part of the string "600sp"?

查看:170
本文介绍了获取字符串“600sp”的整数部分的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,说600sp,我希望从中得到整数部分(600)。

I have a string, say "600sp" from which I wish to obtain the integer part (600).

如果我做整数.valueOf(600sp)由于字符串中遇到非数字值s,我收到异常。

If I do Integer.valueOf("600sp") I get an exception due to the non-numeric value "s" which is encountered in the string.

获取整数部分最快最干净的方法是什么?

What is the fastest cleanest way to grab the integer part?

谢谢!

推荐答案

如果您的字符串格式总是如此是数字后跟一些字符,然后尝试这个

If your string format is always going to be number followed by some characters, then try this

mystr.split("[a-z]")[0]

这篇关于获取字符串“600sp”的整数部分的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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