Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,914 questions

51,847 answers

573 users

How to extract the last number from a string in C#

3 Answers

0 votes
using System;
using System.Linq;

class Program
{
    static void Main() {
        string str = "98 c# programming 15";
        
        int number = int.Parse(str.Split().Last());

        Console.Write(number);
    }
}




/*
run:

15

*/

 



answered Aug 30, 2023 by avibootz
0 votes
using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main() {
        string str = "98 c# programming15";
        
        string[] arr = Regex.Split(str, @"\D+");
        
        int number = int.Parse(arr[arr.Length - 1]);

        Console.Write(number);
    }
}




/*
run:

15

*/

 



answered Aug 30, 2023 by avibootz
0 votes
using System;
using System.Text.RegularExpressions;

class Program
{
    public static int get_last_number(String str) {
	    string[] arr = Regex.Split(str, @"\D+");

		return int.Parse(arr[arr.Length - 1]);
	}
    static void Main() {
        string str = "98 c# 22 programming15";

        int number = get_last_number(str);

        Console.Write(number);
    }
}




/*
run:

15

*/

 



answered Aug 30, 2023 by avibootz

Related questions

2 answers 146 views
3 answers 270 views
1 answer 153 views
1 answer 114 views
2 answers 104 views
1 answer 124 views
...