第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

計(jì)算java字符串中的句子數(shù)

計(jì)算java字符串中的句子數(shù)

慕妹3146593 2021-07-13 17:14:11
我想計(jì)算一個(gè)字符串中的句子數(shù),到目前為止我正在使用這個(gè):int count = str.split("[!?.:]+").length;但我的字符串包含“?!?nbsp;例如在名字和單詞之間“他的名字是 Walton DC,他去年剛剛完成了他的 B.Tech。”現(xiàn)在使用上面的行作為示例 count 將返回 4 個(gè)句子,但只有一個(gè)。那么如何應(yīng)對(duì)這些情況呢?
查看完整描述

3 回答

?
森欄

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超5個(gè)贊

您可以使用BreakIterator,并檢測(cè)不同類(lèi)型的文本邊界


在你的情況下句子:


private static void markBoundaries(String target, BreakIterator iterator) {

    StringBuffer markers = new StringBuffer();

    markers.setLength(target.length() + 1);

    for (int k = 0; k < markers.length(); k++) {

        markers.setCharAt(k, ' ');

    }

    int count = 0;

    iterator.setText(target);

    int boundary = iterator.first();

    while (boundary != BreakIterator.DONE) {

        markers.setCharAt(boundary, '^');

        ++count;

        boundary = iterator.next();

    }

    System.out.println(target);

    System.out.println(markers);

    System.out.println("Number of Boundaries: " + count);

    System.out.println("Number of Sentences: " + (count-1));

}


public static void main(String[] args) {

    Locale currentLocale = new Locale("en", "US");

    BreakIterator sentenceIterator

            = BreakIterator.getSentenceInstance(currentLocale);

    String someText = "He name is Walton D.C. and he just completed his B.Tech last year.";

    markBoundaries(someText, sentenceIterator);

    someText = "This order was placed for QT3000! MK?";

    markBoundaries(someText, sentenceIterator);


}

輸出將是:


He name is Walton D.C. and he just completed his B.Tech last year.

^                                                                 ^

Number of Boundaries: 2

Number of Sentences: 1

This order was placed for QT3000! MK?

^                                 ^  ^

Number of Boundaries: 3

Number of Sentences: 2


查看完整回答
反對(duì) 回復(fù) 2021-07-23
?
繁星coding

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超4個(gè)贊

解決方案可能是在點(diǎn)的情況下,您可以檢查后面是否有空格和大寫(xiě)字母。


“[點(diǎn)][空格][大寫(xiě)字母]”


這將是對(duì)判決的肯定


更新相同的代碼:


public static void main( String args[] ) {

      // String to be scanned to find the pattern.

      String line = "This order was placed for QT3000! MK? \n Thats amazing. \n But I am not sure.";

  String pattern = "([.!?])([\\s\\n])([A-Z]*)";


  // Create a Pattern object

  Pattern r = Pattern.compile(pattern);


  // Now create matcher object.

  Matcher m = r.matcher(line);

  int count=0;

  while (m.find( )) {

      count++;

  }

  count++; //for the last line, which will not get included here.

  System.out.println("COUNT=="+count);

}


查看完整回答
反對(duì) 回復(fù) 2021-07-23
?
元芳怎么了

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超7個(gè)贊

簡(jiǎn)單的方法


公共類(lèi)計(jì)數(shù)線{


public static void main(String[] args) {

    // TODO Auto-generated method stub

    String s="Find the number Sentence";

    int count=0;

    for (int i = 0; i < s.length(); i++) {

        if(s.charAt(i)==' ') {

            count++;

        }

    }

    count=count+1;

    System.out.println(count);

}

}


查看完整回答
反對(duì) 回復(fù) 2021-07-23
  • 3 回答
  • 0 關(guān)注
  • 184 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)