Java 如何在字符串中查找空白?

Java 如何在字符串中查找空白?,java,string,space,Java,String,Space,如何检查字符串是否包含空白字符、空格或“”。如果可能,请提供一个Java示例 例如:String=“测试单词”您可以使用正则表达式确定是否有空格字符\s regex的更多信息。要检查字符串是否包含空格,请使用并调用其find方法 Pattern pattern = Pattern.compile("\\s"); Matcher matcher = pattern.matcher(s); boolean found = matcher.find(); 如果要检查它是否只包含空

如何检查字符串是否包含空白字符、空格或“”。如果可能,请提供一个Java示例


例如:
String=“测试单词”

您可以使用正则表达式确定是否有空格字符<代码>\s

regex的更多信息。

要检查字符串是否包含空格,请使用并调用其find方法

Pattern pattern = Pattern.compile("\\s");
Matcher matcher = pattern.matcher(s);
boolean found = matcher.find();
如果要检查它是否只包含空格,则可以使用:


这将告诉您是否存在任何空白:

通过循环:

for (char c : s.toCharArray()) {
    if (Character.isWhitespace(c)) {
       return true;
    }
}


StringUtils.isBlank(s)
将告诉您是否只有白色密码。

检查字符串是否至少包含一个空格字符:

public static boolean containsWhiteSpace(final String testCode){
    if(testCode != null){
        for(int i = 0; i < testCode.length(); i++){
            if(Character.isWhitespace(testCode.charAt(i))){
                return true;
            }
        }
    }
    return false;
}

在Unicode支持方面也要彻底得多。

使用此代码对我来说是更好的解决方案

public static boolean containsWhiteSpace(String line){
    boolean space= false; 
    if(line != null){


        for(int i = 0; i < line.length(); i++){

            if(line.charAt(i) == ' '){
            space= true;
            }

        }
    }
    return space;
}
public静态布尔containsWhiteSpace(字符串行){
布尔空间=假;
如果(行!=null){
对于(int i=0;i
使用Apache Commons:

import java.util.Scanner;
公营骆驼案{
公共静态void main(字符串[]args)
{
扫描仪用户输入=新扫描仪(System.in);
字符串行1;
Line1=user_input.nextLine();
int j=1;
//现在读这行中的每个单词,并将其转换为驼峰大小写
字符串result=“”,result1=“”;
对于(int i=0;i
使用org.apache.commons.lang.StringUtils

  • 搜索空白
  • boolean with whitespace=StringUtils.contains(“我的名字”,“我的名字”)

  • 删除字符串中的所有空白
  • StringUtils.deleteWhitespace(null)=null StringUtils.deleteWhitespace(“”=“” StringUtils.deleteWhitespace(“abc”)=“abc” StringUtils.deleteWhitespace(“ab c”)=“abc”


    我想向您介绍一种非常简单的方法:

    一个小的用法示例:

    public static void main(String[] args) {
        System.out.println(containWhitespace("i love potatoes"));
        System.out.println(containWhitespace("butihatewhitespaces"));
    }
    
    输出:

    true
    false
    
    可以使用函数查找字符串中的空格

     public class Test {
      public static void main(String args[]) {
       String fav="Hi Testing  12 3";
       int counter=0;
       for( int i=0; i<fav.length(); i++ ) {
        if(fav.charAt(i) == ' ' ) {
         counter++;
          }
         }
        System.out.println("Number of spaces "+ counter);
        //This will print Number of spaces 4
       }
      }
    
    公共类测试{
    公共静态void main(字符串参数[]){
    字符串fav=“Hi Testing 12 3”;
    int计数器=0;
    
    对于(int i=0;i可能我更新最新的答案已经晚了。您可以使用以下解决方案之一:

    public static boolean containsWhiteSpace(final String input) {
            if (isNotEmpty(input)) {
                for (int i = 0; i < input.length(); i++) {
                    if (Character.isWhitespace(input.charAt(i)) || Character.isSpaceChar(input.charAt(i))) {
                        return true;
                    }
                }
            }
            return false;
        }
    

    什么是“空白”?它是“?@Dominic-它是当你清空“空白”时得到的。Duh!!!空字符串包含空格吗?bool应该是布尔的。谢谢你,这是我想要的。很好的综合答案(+1),但是循环变量可以在不创建新字符数组的情况下完成(请参阅我的答案)@seanizer true.+1对您来说。对于:)注意:isWhitespace方法在考虑中不使用任何分隔符我猜此代码不完全支持unicode,因为:
    此方法无法处理补充字符。若要支持所有unicode字符,包括补充字符,请使用isWhitespace(int)方法。
    True,但我想知道在
    0xffff
    之外的Unicode范围内定义了多少新的空格字符。我完全不知道,我提到它只是为了完整性。使用
    Character.isWhitespace(testCode.charAt(I))| | Character.isSpaceChar(testCode.charAt(I))
    相反。请解释您为什么这样做以及为什么这是解决方案。请注释您的代码!此代码没有找到许多空白。您可以通过提供使用此正则表达式的Java代码来改进此答案。此答案不会检查所有空白。为了清楚起见,请在代码中添加一个示例,如注释。此代码不是e高效,而且它也不是unicode编码,它只检测空格,而不是所有空格。
    StringUtils.containsWhitespace(str)
    
    package com.test;
    
    public class Test {
    
        public static void main(String[] args) {
    
            String str = "TestCode ";
            if (str.indexOf(" ") > -1) {
                System.out.println("Yes");
            } else {
                System.out.println("Noo");
            }
        }
    }
    
    import java.util.Scanner;
    public class camelCase {
    
    public static void main(String[] args)
    {
        Scanner user_input=new Scanner(System.in);
        String Line1;
        Line1 = user_input.nextLine();
        int j=1;
        //Now Read each word from the Line and convert it to Camel Case
    
        String result = "", result1 = "";
        for (int i = 0; i < Line1.length(); i++) {
            String next = Line1.substring(i, i + 1);
            System.out.println(next + "  i Value:" + i + "  j Value:" + j);
            if (i == 0 | j == 1 )
            {
                result += next.toUpperCase();
            } else {
                result += next.toLowerCase();
            }
    
            if (Character.isWhitespace(Line1.charAt(i)) == true)
            {
                j=1;
            }
            else
            {
                j=0;
            }
        }
        System.out.println(result);
    
    String str = "Test Word";
                if(str.indexOf(' ') != -1){
                    return true;
                } else{
                    return false;
                }
    
    public static boolean containWhitespace(String value) {
        return value.contains(" ");
    }
    
    public static void main(String[] args) {
        System.out.println(containWhitespace("i love potatoes"));
        System.out.println(containWhitespace("butihatewhitespaces"));
    }
    
    true
    false
    
     public class Test {
      public static void main(String args[]) {
       String fav="Hi Testing  12 3";
       int counter=0;
       for( int i=0; i<fav.length(); i++ ) {
        if(fav.charAt(i) == ' ' ) {
         counter++;
          }
         }
        System.out.println("Number of spaces "+ counter);
        //This will print Number of spaces 4
       }
      }
    
    public static boolean containsWhiteSpace(final String input) {
            if (isNotEmpty(input)) {
                for (int i = 0; i < input.length(); i++) {
                    if (Character.isWhitespace(input.charAt(i)) || Character.isSpaceChar(input.charAt(i))) {
                        return true;
                    }
                }
            }
            return false;
        }
    
    public static boolean containsWhiteSpace(final String input) {
            return CharMatcher.whitespace().matchesAnyOf(input);
        }