【问题标题】:How can I find whitespace in a String?如何在字符串中找到空格?
【发布时间】:2011-05-03 08:10:34
【问题描述】:

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

例如:String = "test word";

【问题讨论】:

  • 什么是“空白空间”?是“”吗?
  • @Dominic - 当你清空“完整空间”时,你会得到什么。呃!!!

标签: java string space


【解决方案1】:

您可以使用正则表达式来确定是否有空格字符。 \s.

正则表达式here的更多信息。

【讨论】:

  • 您可以通过提供使用此正则表达式的 Java 代码来改进此答案。
【解决方案2】:

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

Pattern pattern = Pattern.compile("\\s");
Matcher matcher = pattern.matcher(s);
boolean found = matcher.find();

如果你想检查它是否只包含空格,那么你可以使用String.matches:

boolean isWhitespace = s.matches("^\\s*$");

【讨论】:

  • 空字符串是否包含空格?我认为 bool 应该是布尔值。
  • \\s 是我要找的,谢谢
【解决方案3】:
public static void main(String[] args) {
    System.out.println("test word".contains(" "));
}

【讨论】:

  • 此答案不检查所有空格。
【解决方案4】:

这会告诉你是否有任何个空格:

通过循环:

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

s.matches(".*\\s+.*")

StringUtils.isBlank(s) 会告诉你是否有只有个空白。

【讨论】:

  • 不错的综合答案 (+1),但无需创建新的 char 数组即可完成循环变体(请参阅我的答案)
  • @seanizer true。 +1 给你。为此:)
  • 注意:isWhitespace 方法不考虑不间断空格
【解决方案5】:

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

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;
}

参考:


使用Guava 库,就简单多了:

return CharMatcher.WHITESPACE.matchesAnyOf(testCode);

CharMatcher.WHITESPACE 在 Unicode 支持方面也更加彻底。

【讨论】:

  • 我猜这段代码并不完全支持 unicode,因为:This method cannot handle supplementary characters. To support all Unicode characters, including supplementary characters, use the isWhitespace(int) method.
  • 是的,但我想知道在0xffff 之外的 Unicode 范围内定义了多少新的空白字符...
  • 我完全不知道,我只是为了完整性才提到它。
  • 改用Character.isWhitespace(testCode.charAt(i)) || Character.isSpaceChar(testCode.charAt(i))
【解决方案6】:

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

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;
}

【讨论】:

  • 代码效率不高,也不支持 unicode
  • 而且它只检测空格,而不是所有的空格。
【解决方案7】:

使用 Apache Commons StringUtils:

StringUtils.containsWhitespace(str)

【讨论】:

    【解决方案8】:
    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");
            }
        }
    }
    

    【讨论】:

    • 请解释您为什么这样做以及为什么这是解决方案。评论你的代码!
    • 这段代码找不到很多空格。
    【解决方案9】:
    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);
    

    【讨论】:

      【解决方案10】:

      使用 org.apache.commons.lang.StringUtils。

      1. 搜索空格

      boolean withWhiteSpace = StringUtils.contains("我的名字", " ");

      1. 删除字符串中的所有空格

      StringUtils.deleteWhitespace(null) = null StringUtils.deleteWhitespace("") = "" StringUtils.deleteWhitespace("abc") = "abc" StringUtils.deleteWhitespace("ab c") = "abc"

      【讨论】:

        【解决方案11】:
        String str = "Test Word";
                    if(str.indexOf(' ') != -1){
                        return true;
                    } else{
                        return false;
                    }
        

        【讨论】:

        • 为了清楚起见,请在代码中添加一个示例,如注释。
        【解决方案12】:

        我给你一个使用String.contains的非常简单的方法:

        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
        

        【讨论】:

          【解决方案13】:

          您可以使用charAt() 函数查找字符串中的空格。

           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
             }
            }
          

          【讨论】:

            【解决方案14】:

            也许我迟到了最新的答案。您可以使用以下解决方案之一:

            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);
                }
            

            【讨论】:

              猜你喜欢
              • 2012-03-06
              • 2011-02-02
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-12-09
              • 2018-06-05
              相关资源
              最近更新 更多