如何在String中找到空格?

问题描述 投票:51回答:14

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

例如:String = "test word";

java string space
14个回答
82
投票

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

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

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

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

0
投票
String str = "Test Word";
            if(str.indexOf(' ') != -1){
                return true;
            } else{
                return false;
            }

0
投票

我的目的是一个使用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

0
投票

你基本上可以这样做

if(s.charAt(i)==32){
   return true;
}

你必须编写boolean方法.Whitespace char是32。


0
投票

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

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

-1
投票
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");
        }
    }
}

23
投票

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

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

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


20
投票

这将告诉您是否有任何空格:

通过循环:

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

要么

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

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


8
投票

使用Apache Commons StringUtils

StringUtils.containsWhitespace(str)

3
投票

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

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

2
投票
public static void main(String[] args) {
    System.out.println("test word".contains(" "));
}

2
投票

您可以使用Regex来确定是否存在空格字符。 \s

更多信息正则表达式here


0
投票
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);

0
投票

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

  1. 搜索空格

boolean withWhiteSpace = StringUtils.contains(“my name”,“”);

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

StringUtils.deleteWhitespace(null)= null StringUtils.deleteWhitespace(“”)=“”StringUtils.deleteWhitespace(“abc”)=“abc”StringUtils.deleteWhitespace(“ab c”)=“abc”

© www.soinside.com 2019 - 2024. All rights reserved.