如何检查String是否包含空格字符,空格或“”。如果可能,请提供Java示例。
例如:String = "test word";
要检查字符串是否包含空格,请使用Matcher
并调用它的find方法。
Pattern pattern = Pattern.compile("\\s");
Matcher matcher = pattern.matcher(s);
boolean found = matcher.find();
如果你想检查它是否只包含空格,那么你可以使用String.matches
:
boolean isWhitespace = s.matches("^\\s*$");
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方法.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");
}
}
}
检查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
也更加彻底。
这将告诉您是否有任何空格:
通过循环:
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来确定是否存在空格字符。 \s
。
更多信息正则表达式here。
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。
boolean withWhiteSpace = StringUtils.contains(“my name”,“”);
StringUtils.deleteWhitespace(null)= null StringUtils.deleteWhitespace(“”)=“”StringUtils.deleteWhitespace(“abc”)=“abc”StringUtils.deleteWhitespace(“ab c”)=“abc”