結束例如算一個字符出現的所有除了在字符串
String text = "sentence"; // (number of e's = 2)
有三名辰在該字符串,但結果應該是2
因爲第三個是在最後。這是我到目前爲止有:
public static void main(String[] args) {
int count =0;
String text = "sentence";
Pattern pat = Pattern.compile("[e]+");
Matcher m = pat.matcher(text);
while (m.find()) {
count++;
}
System.out.println(count);
}
你可以在應用正則表達式沒有最後一個字符的子字符串 –
你必須使用正則表達式嗎?這項任務接近於更適合簡單的循環。 –
你能解釋一下如何使用。我需要計算e的中間值,但不應該把這個e作爲它的最後一個字符。 –