Как найти пробел в строке? - PullRequest
51 голосов
/ 01 ноября 2010

Как проверить, содержит ли строка символ пробела, пробел или символ "". Если возможно, приведите пример Java.

Например: String = "test word";

Ответы [ 14 ]

0 голосов
/ 28 октября 2017
String str = "Test Word";
            if(str.indexOf(' ') != -1){
                return true;
            } else{
                return false;
            }
0 голосов
/ 04 апреля 2016

Используйте org.apache.commons.lang.StringUtils.

  1. для поиска пробелов

логическое значение withWhiteSpace = StringUtils.contains ("мое имя", "");

Чтобы удалить все пробелы в строке

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

0 голосов
/ 16 февраля 2016
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 голосов
/ 16 марта 2015
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");
        }
    }
}
...