NET如何從string中挖出所有的number?

這篇文章主要講解了“NET如何從 string 中挖出所有的 number ?”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“NET如何從 string 中挖出所有的 number ?”吧!

創(chuàng)新互聯(lián)公司服務(wù)項目包括靈璧網(wǎng)站建設(shè)、靈璧網(wǎng)站制作、靈璧網(wǎng)頁制作以及靈璧網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,靈璧網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到靈璧省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

 

咨詢區(qū)

  • van:

我現(xiàn)在有一個需求,想從 string 中找到所有的 number 并提取出來。

舉例如下:


string test = "1 hello"
string test1 = " 1 world"
string test2 = "helloworld 99"

 

請問我該如何做?

 

回答區(qū)

  • Tabares:

這個簡單,可以用正則表達(dá)式 Regex.Split 提取所有的 number,使用下面的代碼。


    public class Program
    {
        static void Main(string[] args)
        {
            string input = "There are 4 numbers in this string: 40, 30, and 10.";

            // Split on one or more non-digit characters.
            string[] numbers = Regex.Split(input, @"\D+");
            
            foreach (string value in numbers)
            {
                if (!string.IsNullOrEmpty(value))
                {
                    int i = int.Parse(value);
                    Console.WriteLine("Number: {0}", i);
                }
            }
        }
    }

 
NET如何從 string 中挖出所有的 number ?  

  • Ramireddy Ambati:

可以試著用 Regex.Matches 提取。


        static void Main(string[] args)
        {
            string input = "Hello 20, I am 30 and he is 40";

            var numbers = Regex.Matches(input, @"\d+").OfType<Match>().Select(m => int.Parse(m.Value)).ToArray();

            foreach (var item in numbers)
            {
                Console.WriteLine($"number: {item}");
            }
        }

 
NET如何從 string 中挖出所有的 number ?  

  • Thomas C. G. de Vilhena:

我寫了一個擴(kuò)展方法可以提取出 string 中所有的正整數(shù),方法如下:


    public static class StringExt
    {
        public static List<long> Numbers(this string str)
        {
            var nums = new List<long>();
            var start = -1;
            for (int i = 0; i < str.Length; i++)
            {
                if (start < 0 && Char.IsDigit(str[i]))
                {
                    start = i;
                }
                else if (start >= 0 && !Char.IsDigit(str[i]))
                {
                    nums.Add(long.Parse(str.Substring(start, i - start)));
                    start = -1;
                }
            }
            if (start >= 0)
                nums.Add(long.Parse(str.Substring(start, str.Length - start)));
            return nums;
        }
    }

 

然后像下面這樣調(diào)用

        public static void Main(string[] args)
        {
            var input = "I was born in 1989, 27 years ago from now (2016)";

            foreach (var item in input.Numbers())
            {
                Console.WriteLine($"number: {item}");
            }
        }
 
NET如何從 string 中挖出所有的 number ?    

感謝各位的閱讀,以上就是“NET如何從 string 中挖出所有的 number ?”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對NET如何從 string 中挖出所有的 number ?這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

網(wǎng)站欄目:NET如何從string中挖出所有的number?
網(wǎng)頁路徑:http://muchs.cn/article16/joghdg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、搜索引擎優(yōu)化、域名注冊服務(wù)器托管、外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)