Java 如何检查输入字符串是否包含空格?
我有一个输入对话框,要求输入XML元素名,我想检查它是否有空格 我可以执行类似name.matches()的操作吗?为什么要使用正则表达式Java 如何检查输入字符串是否包含空格?,java,regex,Java,Regex,我有一个输入对话框,要求输入XML元素名,我想检查它是否有空格 我可以执行类似name.matches()的操作吗?为什么要使用正则表达式 name.contains(" ") 这应该同样有效,而且速度更快。如果你真的想要一个正则表达式,你可以使用这个: str.matches(".*([ \t]).*") 在某种意义上,与此正则表达式匹配的所有内容都不是有效的xml标记名: if(str.matches(".*([ \t]).*")) print "the input str
name.contains(" ")
这应该同样有效,而且速度更快。如果你真的想要一个正则表达式,你可以使用这个:
str.matches(".*([ \t]).*")
在某种意义上,与此正则表达式匹配的所有内容都不是有效的xml标记名:
if(str.matches(".*([ \t]).*"))
print "the input string is not valid"
会(稍微)快一些。如果使用正则表达式,它已经为任何非空白字符预定义了字符类“\S”
!str.matches("\\S+")
告诉您这是否是一个至少包含一个字符的字符串,其中所有字符都是非空白的 使用这些代码检查字符串是否包含空格/空格可能位于第一个位置、中间位置或最后一个位置:
name = firstname.getText().toString(); //name is the variable that holds the string value
Pattern space = Pattern.compile("\\s+");
Matcher matcherSpace = space.matcher(name);
boolean containsSpace = matcherSpace.find();
if(constainsSpace == true){
//string contains space
}
else{
//string does not contain any space
}
一个与前面类似的简单答案是:
str.matches(".*\\s.*")
- 第一个“*”表示空格前面可以有任何字符的零个或多个实例李>
- “\\s”表示它必须包含任何空白字符
- 最后一个“*”表示空格后的任何字符都可以有零个或多个实例
boolean containsWhitespace(String str){
return str.matches(".*\\s.*");
}
String[] testStrings = {"test", " test", "te st", "test ", "te st",
" t e s t ", " ", "", "\ttest"};
for (String eachString : testStrings) {
System.out.println( "Does \"" + eachString + "\" contain whitespace? " +
containsWhitespace(eachString));
}
您可以使用此代码检查输入字符串是否包含空格
public static void main(String[]args)
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the string...");
String s1=sc.nextLine();
int l=s1.length();
int count=0;
for(int i=0;i<l;i++)
{
char c=s1.charAt(i);
if(c==' ')
{
System.out.println("spaces are in the position of "+i);
System.out.println(count++);
}
else
{
System.out.println("no spaces are there");
}
}
publicstaticvoidmain(字符串[]args)
{
扫描仪sc=新的扫描仪(System.in);
System.out.println(“输入字符串…”);
字符串s1=sc.nextLine();
int l=s1.length();
整数计数=0;
对于(int i=0;i要检查字符串是否不包含任何空格,可以使用
string.matches("^\\S*$")
例如:
"name" -> true
" " -> false
"name xxname" -> false
您可以使用正则表达式“\\s”
计算空格数的示例程序(Java 9及以上版本)
对于Java 8及以下版本可以在while循环中使用matcher.find()并增加计数。例如
int count = 0;
while (matcher.find()) {
count ++;
}
我可以使用这个:boolean space=name.matches(“.*\\s+.”);@CheaIndian No。不必要的正则表达式是不好的。@Chea:很遗憾,你不能。但是你可以对多个答案进行上选。如果只有一个空格,这是有效的。但是,如果文本字符串有多个连续的空格(“”),该怎么办?使用正则表达式也可以为类似情况提供故障保护。@AbhineetKumar你是什么意思?这对我来说很有效,stackoverflow不会呈现多个空格,但它也可以呈现多个空格->String with space=“foo foo”;assert(with space.contains(“”);
这匹配所有空格字符?我们使用正则表达式,因为世界上有太多空格字符。在中文和日文中,您可能只会遇到表意空格,而且永远不会匹配“”。因此,您可以从第一天开始使用正则表达式,并知道它是完全正确的。str.matches(\\s+))@Frank,不,它与只包含1个或多个空格字符的字符串相匹配。这对于检查用户名中的空格非常有用。回答很好,但它错误地发现空字符串包含空格。它可以轻松地与长度检查相结合,例如(!str.matches(“\\S+”)&(str.length()>0))作为一个完全正确的解决方案,此代码不符合OP的要求,并且存在缺陷。它似乎试图重新实现字符串类的标准方法contains
,该方法只打印大量冗余输出,而不是返回布尔值。
"name" -> true
" " -> false
"name xxname" -> false
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("\\s", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher("stackoverflow is a good place to get all my answers");
long matchCount = matcher.results().count();
if(matchCount > 0)
System.out.println("Match found " + matchCount + " times.");
else
System.out.println("Match not found");
}
}
int count = 0;
while (matcher.find()) {
count ++;
}