質問

文字列に空白の文字、空のスペース、または「」が含まれているかどうかを確認するにはどうすればよいですか。可能であれば、Javaの例を提供してください。

例えば: String = "test word";

役に立ちましたか?

解決

文字列のかどうかを確認します Whitespaceが含まれています 使う Matcher そして、それを見つけられる方法と呼んでください。

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

あなたがそれかどうかを確認したい場合 空白のみで構成されています その後、使用できます String.matches:

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

他のヒント

文字列には、少なくとも1つの空白文字が含まれているかどうかを確認します。

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

参照:


を使用して グアバ 図書館、それははるかに簡単です:

return CharMatcher.WHITESPACE.matchesAnyOf(testCode);

CharMatcher.WHITESPACE また、Unicodeサポートに関しては、はるかに徹底的です。

これにより、あなたがいるかどうかがわかります どれか ホワイトスペース:

ループによって:

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

また

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

StringUtils.isBlank(s) あるかどうか教えてくれます それだけ ホワイトスペース。

Apache Commonsを使用します stringutils:

StringUtils.containsWhitespace(str)

このコードを使用して、私にとってより良い解決策でした。

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 static void main(String[] args) {
    System.out.println("test word".contains(" "));
}

Regexを使用して、Whitespace文字があるかどうかを判断できます。 \s.

Regexの詳細 ここ.

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

org.apache.commons.lang.stringutilsを使用します。

  1. 空白を検索します

whitespace = stringutils.contains( "my name"、 "");

  1. 文字列内のすべてのホワイトスペースを削除するには

stringutils.deletewhiteSpace(null)= null stringutils.deletewhiteSpace( "")= "" stringutils.deletewhitespace( "abc")= "abc" stringutils.deletewhitespace( "ab c)=" abc "

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

私はあなたに使用する非常に簡単な方法をあなたに目的としています 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

基本的にこれを行うことができます

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

boolean method.whitespace charは32を記述する必要があります。

使用できます 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
   }
  }
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");
        }
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top