java查找字符串格式的数字中的字符
如何在数字中找到字符串
下面是一个简单的例子
private char check() {
String sample ="1212kkk";//121hhh444 | 12-22
return 'k';//'h' | '-'
}
如果不是数字,我想返回那个值
我怎样才能从这个戒指上得到第一个角色
你可以在下面搜索框中键入要查询的问题!
如何在数字中找到字符串
下面是一个简单的例子
private char check() {
String sample ="1212kkk";//121hhh444 | 12-22
return 'k';//'h' | '-'
}
如果不是数字,我想返回那个值
我怎样才能从这个戒指上得到第一个角色
# 1 楼答案
我有什么不对劲吗?如果将某些内容保存为int(数字),则其中不能包含字符串值。但是,如果您的意思是,您有一个字符串,在它的字符串编号中,并且只想获取数字,那么这个regex命令将获取所有数字
# 2 楼答案
试试番石榴
CharMatcher.indexIn
比如:
if(CharMatcher.JAVA_LETTER.indexIn(yourString) != -1) return yourString.charAt(CharMatcher.JAVA_LETTER.indexIn(yourString));
打印
a
# 3 楼答案
您需要更改方法的签名,否则调用方将无法判断字符串何时为“良好”(即仅包含数字)。一种方法是返回^{} ,它是
char
原语的包装器在内部,您可以使用简单的正则表达式
[^0-9]
来匹配String
中的第一个非数字。当没有匹配项时,返回null
。这样,调用者就可以像这样调用您的方法:# 4 楼答案
试试这个:
# 5 楼答案
# 6 楼答案
\D是非数字,因此\D*是一行中任意数量的非数字。所以您的整个字符串应该匹配\D*
请用
\\D*
和\D*
试试