Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java endsWith()方法始终返回false_Java_Regex - Fatal编程技术网

Java endsWith()方法始终返回false

Java endsWith()方法始终返回false,java,regex,Java,Regex,我在sketchware上,这是一款android应用程序,用于创建应用程序。所有内容都是以块为单位制作的,但都是用纯普通java编译的 我似乎找不到问题,但是endsWith方法总是返回false。即使它以0、6或其他任何形式结束 if (!textview1.getText().toString().isEmpty() == true) { // the expression in the following if statement never evaluates to true

我在sketchware上,这是一款android应用程序,用于创建应用程序。所有内容都是以块为单位制作的,但都是用纯普通java编译的

我似乎找不到问题,但是
endsWith
方法总是返回
false
。即使它以0、6或其他任何形式结束

if (!textview1.getText().toString().isEmpty() == true) {
    // the expression in the following if statement never evaluates to true
    if (textview1.toString().endsWith("[10-9]") == true) {
        symbols.add("-");
        textview1.setText(textview1.getText().toString().concat("-"));
    } else {
        SketchwareUtil.showMessage(getApplicationContext(), "false");
    }
} else {
        SketchwareUtil.showMessage(getApplicationContext(), "Can't use symbols before numbers");
}
检查文档

endsWith
中的参数应为字符序列。字符串的结尾不能有字符序列,
[0-9]
,因此结果总是
false

你应该这样写:

if(textview1.toString().matches("^.*\\d$"))
注意:如果字符串末尾有数字,则返回
true
。如果希望它返回
true
如果字符串中的任何位置都有一个数字,则应根据
endsWith
方法使用
匹配(.*\\d.*)

测试此字符串是否以指定的后缀结尾

因此,如果您将例如
test1
作为字符串传递,
endsWith
将计算为false,因为
test1
不会以
[0-9]
结尾

你需要使用正则表达式。使用
字符串
类中的
匹配项

例如:

 String test = "test2";
 System.out.println(test.endsWith("[0-9]")); //false
 System.out.println(test.matches(".*[0-9]$")); //true

正则表达式可能是使其正确工作的最佳选择。类似于此:

Pattern.compile("[0-9]$").matcher(textView1.toString()).find())

如果字符串中的最后一个字符与0-9匹配,则返回true或false。

endsWith()
的后缀不是正则表达式。这只是一个子串。哦。。。我如何检查多个后缀?伙计们,会有人犯同样的错误<代码>字符串方法有时采用reg exp字符串,有时则不采用。所以,要么找个傻瓜,要么允许这个问题,因为它对我来说似乎完全有效。谢谢!我只想检查方法中的任何数字。我该怎么做呢。检查字符串是否以数字结尾。我是
find
的支持者,尽管我认为使用
Pattern
Matcher
对于这个简单的任务来说有点过分。但是,要提取数字,我会使用
find
“\\d+$”
在末尾查找数字字符串(或不查找)。