回答
用於檢查字符串是否包含空格使用Matcher
並將其稱爲find方法。
Pattern pattern = Pattern.compile("\\s");
Matcher matcher = pattern.matcher(s);
boolean found = matcher.find();
如果你想檢查它是否只包含空格那麼你可以使用String.matches
:
boolean isWhitespace = s.matches("^\\s*$");
空字符串是否包含空格?布爾應該是布爾值,我認爲。 – Joni 2010-11-01 09:52:40
\\ s就是我在找的東西,謝謝 – Vivekanand 2013-04-03 05:34:35
public static void main(String[] args) {
System.out.println("test word".contains(" "));
}
這個答案不檢查所有的空格。 – 2015-04-27 13:41:55
這會告訴你,如果有任何空格:
通過循環:
for (char c : s.toCharArray()) {
if (Character.isWhitespace(c)) {
return true;
}
}
或
s.matches(".*\\s+.*")
而且StringUtils.isBlank(s)
會告訴你,如果有只有 whitepsaces。
尼斯綜合答案(+1),但循環變量可以在不創建新的字符數組的情況下完成(請參閱我的回答) – 2010-11-01 09:53:50
@seanizer true。 +1給你。爲此:) – Bozho 2010-11-01 09:58:40
檢查是否字符串包含至少一個空白字符:
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);
CharMatcher.WHITESPACE
也有很多更徹底,當涉及到Unicode支持。
我想這個代碼是不完全unicode知道,因爲:'這種方法不能處理補充字符。要支持所有Unicode字符,包括補充字符,請使用isWhitespace(int)方法。' – 2010-11-01 10:05:37
確實如此,但是我想知道在超出0xffff的Unicode範圍中定義了多少新的空白字符... – 2010-11-01 10:09:40
我完全不知道,我提到它只是爲了完整性。 – 2010-11-01 10:49:19
使用此代碼,是我有更好的解決方案,當你有四個空格,並且是一個txt文件的導出。
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;
}
使用Apache下議院StringUtils:
StringUtils.containsWhitespace(str)
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");
}
}
}
請解釋你爲什麼這樣做,爲什麼這是解決方案。評論你的代碼! – ReneS 2015-03-16 12:33:10
這段代碼找不到很多空格。 – 2015-04-27 12:44:48
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。
- 搜索空格
布爾withWhiteSpace = StringUtils.contains( 「我的名字」,「「);
- 要在串
StringUtils.deleteWhitespace(空)= 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;
}
要清楚,請在代碼中添加一個示例,如評論。 – pirs 2017-10-28 04:20:43
我的目的誰給你使用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;
}
您必須編寫布爾方法。空格字符爲32.
- 1. 我如何找到字符串之間的空白ColdFusion的
- 2. 如何在字符串ios中添加空白空間
- 3. 找到字符間的空白php
- 4. 檢查javascript中的空白字符串以防止發送空白數據並修剪中間空間
- 5. 字符串 - 查找空值
- 6. 查找字符串中空白行的索引
- 7. 刪除字符串中的空白空間
- 8. 如何將空白字符串轉換爲空白日期?
- 9. 在白色空間分割字符串
- 10. 查找字符串中的空格
- 11. 檢查一個字符串是否包含R中的所有空白空間
- 12. 填寫空白字符串
- 13. 空間字符串中的
- 14. 如何從查詢字符串中刪除空白值params
- 15. 使用getline()讀入字符串尾部的空白空間
- 16. 查找數字列表中的「空白」
- 17. 在mysql中查找空白時間
- 18. 如何刪除標籤之間的空白字符串?
- 19. JS如何檢查空字符串和空間
- 20. 存儲空字符串的Hibernate在DB中創建空白空間。
- 21. 如何讓空最後一組字符串作爲空白的
- 22. 修剪Java中的字符串空白
- 23. 一步檢查字符串的空值和空白
- 24. 查找沒有空字符串的行
- 25. 批量查找和替換字符串添加空白行
- 26. 如何區分pltcl中的空字符串和空字符串?
- 27. 空白字符
- 28. 獲取字符和空白字符之間的子串
- 29. PHP會話字符串包含奇怪的空白/空字符
- 30. 如何查找字符串中第五個空格的位置?
什麼是「空白空間」?是嗎 」」? – 2010-11-01 09:47:38
@Dominic - 當你清空「全部空間」時你會得到什麼。咄! – 2010-11-01 10:54:57