當前位置:成語大全網 - 書法字典 - 如何用java編程查詢壹個單詞在段落中出現的次數?

如何用java編程查詢壹個單詞在段落中出現的次數?

1,方法1

公共的?int?countChar(字符串?str、char?ch)?{

//?將字符串轉換為字符數組。

char【】?chs?=?str . tochararray();

//?定義變量計數存儲字符串的出現次數。

int?數數?=?0;

for(int?我?=?0;我?& lt?chs.lengthi++)?{

if(CHS【I】?==?ch)?{

count++;

}

回歸?數數;

}2.方法2

使用org。apache.com蒙斯。朗。字符串工具類。

字符串?str?=?“沒有嗎?圖片?妳呢。說?答?J8?a“;

int?數數?=?string utils . count matches(str,“a“);

system . out . println(count);望采納!!!