Java 如何在字符串中找到空格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4067809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I find whitespace in a String?
提问by jimmy
How can I check to see if a String contains a whitespace character, an empty space or " ". If possible, please provide a Java example.
如何检查字符串是否包含空格字符、空格或“”。如果可能,请提供 Java 示例。
For example: String = "test word";
例如: String = "test word";
采纳答案by Mark Byers
For checking if a string contains whitespaceuse a Matcher
and call it's find method.
要检查字符串是否包含空格,请使用 aMatcher
并调用它的 find 方法。
Pattern pattern = Pattern.compile("\s");
Matcher matcher = pattern.matcher(s);
boolean found = matcher.find();
If you want to check if it only consists of whitespacethen you can use String.matches
:
如果你想检查它是否只包含空格,那么你可以使用String.matches
:
boolean isWhitespace = s.matches("^\s*$");
回答by 123 456 789 0
回答by hanumant
public static void main(String[] args) {
System.out.println("test word".contains(" "));
}
回答by Bozho
This will tell if you there is anywhitespaces:
这将告诉您是否有任何空格:
Either by looping:
要么通过循环:
for (char c : s.toCharArray()) {
if (Character.isWhitespace(c)) {
return true;
}
}
or
或者
s.matches(".*\s+.*")
And StringUtils.isBlank(s)
will tell you if there are onlywhitepsaces.
并且StringUtils.isBlank(s)
会告诉您是否只有白色空间。
回答by Sean Patrick Floyd
Check whether a String contains at least one white space character:
检查 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;
}
Reference:
参考:
Using the Guavalibrary, it's much simpler:
使用Guava库,就简单多了:
return CharMatcher.WHITESPACE.matchesAnyOf(testCode);
CharMatcher.WHITESPACE
is also a lot more thorough when it comes to Unicode support.
CharMatcher.WHITESPACE
在 Unicode 支持方面也更加彻底。
回答by Gilberto
Use this code, was better solution for me.
使用此代码,对我来说是更好的解决方案。
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;
}
回答by br2000
回答by ayushman das
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");
}
}
}
回答by Madhav Adireddi
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);
回答by Rajasekar Kalisamy
Use org.apache.commons.lang.StringUtils.
使用 org.apache.commons.lang.StringUtils。
- to search for whitespaces
- 搜索空格
boolean withWhiteSpace = StringUtils.contains("my name", " ");
boolean withWhiteSpace = StringUtils.contains("my name", " ");
- To delete all whitespaces in a string
- 删除字符串中的所有空格
StringUtils.deleteWhitespace(null) = null StringUtils.deleteWhitespace("") = "" StringUtils.deleteWhitespace("abc") = "abc" StringUtils.deleteWhitespace(" ab c ") = "abc"
StringUtils.deleteWhitespace(null) = null StringUtils.deleteWhitespace("") = "" StringUtils.deleteWhitespace("abc") = "abc" StringUtils.deleteWhitespace(" ab c ") = "abc"