Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there,

Please how can i extract all numeric values from a string and add up all the extracted values. E.g

string myWord="This a sample text 4 that contains 9 words ";

How do i get out 4 and 9, and then add them up to get 13 as total

Thanks in advance

Yours faithfully
Martin
Posted
Comments
Herman<T>.Instance 18-Jul-11 4:50am    
If your text was: This is a sample Text 4 that contains 19 words.
Should the sum be 23 (4+19) or 14 (4+1+9)
katumbamartin 18-Jul-11 5:51am    
Hi digimanus, the answer should be 23 in that case

Very nice and easy logic here[^] to get all numbers from within a string.
Once you have all the numbers in an array or collection, just add them together.
 
Share this answer
 
v2
Comments
Mohammad A Rahman 18-Jul-11 5:11am    
The solution Abhinav posted is nice :) I also posted another way to get the Sum of extracted values from the string. Please my solution.
Sergey Alexandrovich Kryukov 18-Jul-11 13:17pm    
Yes, this is what I would do if I had a weird fantasy to do it. This is about creating of right data structures, where such things are never needed. My 5 anyway.
--SA
Abhinav S 18-Jul-11 13:24pm    
Thanks SA.
example:

<pre lang="cs">
const string myInteger = "[0-9]&quot;;


private void button1_Click(object sender, EventArgs e)
        {
            String myString = textBox1.Text;
            String somInteger = string.Empty;
            string tmpString = string.Empty;
            Int32 summedValue = 0;
            foreach (char value in textBox1.Text)
            {
                if (value.ToString().Trim().Equals(string.Empty) && !tmpString.Equals(string.Empty))
                {
                    summedValue +=  Convert.ToInt32(tmpString);
                    tmpString = string.Empty;
                }
                if (IsInteger(value))
                    tmpString += value.ToString();
            }
            if (!tmpString.Equals(string.Empty))
                summedValue += Convert.ToInt32(tmpString);
            MessageBox.Show(summedValue.ToString());
        }

        private bool IsInteger(char value)
        {
            return Regex.IsMatch(value.ToString(), myInteger);
        }


don't forget the:
C++
using System.Text.RegularExpressions;
 
Share this answer
 
Comments
Mohammad A Rahman 18-Jul-11 6:47am    
reason of my 5,
It is good solution :)
Hope it helps to solve your problem,
C#
class Program
{
    static void Main(string[] args)
    {
        string myWord = "This a sample text 4 that contains 9 words ";
        string[] numbers = Regex.Split(myWord, @"\D+");
        int parsedValue = 0;
        int result = numbers.SkipWhile(item => string.IsNullOrEmpty(item)).Sum(item => Int32.TryParse(item, out parsedValue) ? parsedValue : parsedValue);
    }
}

:)
 
Share this answer
 
Comments
Herman<T>.Instance 18-Jul-11 6:32am    
I am not that good in lamba expressions, but this sure is a big help
Mohammad A Rahman 18-Jul-11 6:46am    
digimanus, I like your solution. another solution of the problem without lambda might be like below,

static void Main(string[] args)
{
string myWord = "This a sample text 4 that contains 9 words ";
string[] numbers = Regex.Split(myWord, @"\D+");
int parsedValue = 0;
int result = 0;
foreach(string item in numbers)
{
if(string.IsNullOrEmpty(item))
continue;
else
result += Int32.TryParse(item, out parsedValue) ? parsedValue : parsedValue;
}
}
}
johannesnestler 18-Jul-11 11:29am    
cool code
Sergey Alexandrovich Kryukov 18-Jul-11 13:18pm    
Good, my 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900