2012-10-31 36 views
0

我想知道String是否由僅重複一種字符組成。檢測字符串是否僅由一種字符組成

比如我想如果String僅由「B」,因此它會是「BB」,「BBBB」,「bbbbbbbbbbb」的情況下,檢測......

+5

[你有什麼嘗試?](http://whathaveyoutried.com)這並不難。付出一些努力。 – 2012-10-31 22:31:23

+0

空字符串應該如何處理?長度爲1的字符串應該如何處理? (在這兩種情況下,都沒有重複。) –

+0

更具體地說,是否應該由一個字符組成的字符串匹配? – Michael

回答

2

怎麼樣的正則表達式?

String pattern = "([a-zA-Z])\\1*"; 

if (string.matches(pattern)) { 
    // The string contains is made up of the same character... 

} 
+1

但是,例如,「11111」失敗。 –

+0

然後將其從'a-zA-z'改爲別的。這不是最終的解決方案,如果他願意,他可以根據自己的規格定製它。 – Michael

+0

有人想告訴我爲什麼他們投了我票嗎?如果我錯了,我想知道爲什麼。 – Michael

0

獲取串並的第一個位置,其餘位置比較,如果它不改變你有一個字符串只有一個字符類型

boolean getout = true; 
int size = string.length() 

if(string == null || string.isEmpty()) getout = false; 

for(int i = 1; getout && i < size; i++) 
    if(string.charAt(i) != string.charAt(0)) 
     getout = false ; 


// If Getout == true it only have the same character. 
+1

不要忘記首先測試字符串是否爲空!只是在說'。 :) –

+0

不會使用正則表達式匹配會更好嗎? –

+0

@Ted Hopp :)編輯。 – dreamcrash

1

使用使用反向引用

boolean b = Pattern.matches("^(.)\\1+$", "aaaaaaa"); 
//true 

b = Pattern.matches("^(.)\\1+$", "aaaabbbbaaa"); 
//false 

如果你想匹配1個字符字符串以及Pattern

東西,在正則表達式的+更改爲*

+0

你不需要在開始和結束時錨定模式嗎? –

+0

'^ ... $'你是什麼意思?我不確定我是否已經完成了這項工作 –

+0

當您使用反向引用時,您需要將您正在引用的內容完全分組。請參閱下面的解決方案 – Michael

0
String regex = "^" + str.charAt(0) + "+$" 
return str.replaceAll(regex,"").length() == 0 ? true : false; 
+1

如果str從一個正則表達式元字符開始會怎麼樣? – fgb

+1

這是狡猾的想法,以避免反向引用,但它不起作用,因爲你不能依靠你期望的基本多語言平面以外的方式工作。考慮這些'''System.err.println(「a」.equals(String.valueOf(「a」.charAt(0))));'''和''System.err.println(「」。equals (String.valueOf(「」。charAt(0))));''第一個返回true,但第二個返回false。 – kybernetikos

0
private final static Pattern ONE_CHAR_PATTERN = Pattern.compile("(.)\\1*"); 
public static boolean isOneChar(String str) { 
    return ONE_CHAR_PATTERN.matcher(str).matches(); 
} 

我想出了一些這樣的其他方法,但是使用正則表達式(如上)是唯一的簡單的方法,我可以拿出與Unicode的基本多文種以外的字符正確處理平面,如

System.err.println(isOneChar("")); 

當與BMP之外的字符處理,你不能依靠的charAt或長度對字符串返回字符或字符數。

這裏的dreamcrashes的回答可能是什麼樣子,如果它使用Unicode正確處理:

public static boolean isOneChar(String string) { 
    if(string == null || string.isEmpty()) return false; // probably, could also make an argument for empty being true. 
    int startCodePoint = Character.codePointAt(string, 0); 
    int length = string.length(); 
    int position = Character.charCount(startCodePoint); 
    while (position < length) { 
     int thisCodePoint = Character.codePointAt(string, position); 
     if (thisCodePoint != startCodePoint) return false; 
     position += Character.charCount(thisCodePoint); 
    } 
    return true; 
} 

這裏的另一種可能性的基礎上,更換想法通過薩蒂亞吉特建議:

public static boolean isOneChar(String string) { 
    String firstCharacter = string.substring(0, string.offsetByCodePoints(0, 1)); 
    return string.replaceAll(Pattern.quote(firstCharacter), "").length() == 0; 
} 

我想這是效率低下(因爲它在做替換),但是我實際上沒有經過性能測試...

StringTokenizer正確處理unicode字符,所以你也可以做這樣的事情:

public static boolean isOneChar(String string) { 
    String firstChar = string.substring(0, string.offsetByCodePoints(0, 1)); 
    return new StringTokenizer(string, firstChar).countTokens() == 0; 
} 

再次,有沒有真正的任何必要去所有的方式通過串,所以我認爲這是不是正則表達式的解決方案作爲高效。

+0

謝謝。在寫這個答案之前,我沒有真正想到使用chartAt,length或char原語的大多數Java代碼可能是錯誤的。 – kybernetikos

相關問題