簡體   English   中英

確定一個字符串中有多少個數字

[英]Determine how many numbers in a string

我有一個字符串數組,試圖在其中查找每個字符串中的單詞數。 我知道如何查找單詞總數,但是它也包含我不想要的數字。 我已經嘗試過s [i] .matches(“。 \\ d +。 ”),但這只是檢查字符串中是否至少有一個數字。 我正在嘗試查找數字的總數(而不是數字)。 例如,一個字符串為“ IN CONGRESS,1776年7月4日”。 我的代碼返回5個單詞,盡管只有3個,因為它包含2個數字。

總單詞數-帶數字的單詞應該有效。 使用以下正則表達式:

matches(".*[0-9].*")
public static void main(String[] args)
{
    int count = 0;
    for (String str : "IN CONGRESS, July 4, 1776.".split("(\\s+)(\\,)*(\\.)*"))
    {
        if (!str.matches("(.)*(\\d)(.)*"))
        {
            count++;
            System.out.println("word "+count+":"+str);
        }
    }
    System.out.println("Total words:"+count);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM