【发布时间】:2017-05-09 19:34:49
【问题描述】:
假设我有一个字符串a。我想检查a 是否包含减号后的数字。例如,a="-78";
如果 a 仅在减号 (-) 之后有一个数字(此处为 7),那么我可以基于此返回 true 或 false。
【问题讨论】:
-
可能是重复的,你在问之前搜索过吗?
-
是的,我找到了,但没有找到合适的问题
假设我有一个字符串a。我想检查a 是否包含减号后的数字。例如,a="-78";
如果 a 仅在减号 (-) 之后有一个数字(此处为 7),那么我可以基于此返回 true 或 false。
【问题讨论】:
你可以这样做:
return a.matches("-\\d+");
【讨论】:
你可以用startsWith()方法检查字符串:
String a = 0;
a="-78";
// Starts with
boolean b = string.startsWith("-7"); // return true
【讨论】:
假设我有一个字符串 a。我想检查是否包含 减号后的数字。例如,a="-78";
boolean matches = s.matches(".*-\\d+");
这样你就可以匹配a = "-78",也可以匹配a = "anything-78"
【讨论】:
a = "operator-78" 来实现,运算符可以是`'+'、'-'、'*'或'/'`
boolean matches = s.matches("[-+*/]-\\d+");
你可以用这样的简单代码来做到这一点:
String a = "-78";
int b = a.indexOf("-");
if(b == -1)
//there is no "-".
else {
int c = a.charAt(b+1);
if(c >= 48 && c <= 57)
// there is a "-" and after that you have a number.
}
【讨论】: