Solved

Extracting integers from comma separated string

Posted on 2011-09-06
11
379 Views
Last Modified: 2012-05-12
I have a string of comma separated numbers. I want to get the numbers that do not begin with E and put them into separate integers. Here is the string that I have.

E0490,E0493,E0494,E0495,E0497,E0498,94399960,94401460

So I want the last two numbers, however the amount of numbers beginning with E may change and the amount of other numbers may change. So I cannot just find the index of the comma after the last E number and trim the string. I am using C#.

How would I go about doing this?

0
Comment
Question by:tango2009
  • 5
  • 5
11 Comments
 
LVL 18

Expert Comment

by:Cluskitt
Comment Utility
You can split it into an array:

string[] SplitString = MyVar.Split(',')

Then use SplitString.Length to find out how many there are and get the last 2.
0
 
LVL 15

Expert Comment

by:Minh Võ Công
Comment Utility
yes, you can use string[] SplitString = MyVar.Split(',') to split you string to string[]
and in this array you can remove string that beggin with E
0
 

Author Comment

by:tango2009
Comment Utility
That is what I originally tried, but sometimes the string may have three non E numbers or four. How do I get around this? Is there a way I can test the length of each element in the array and pull the ones out which are over a certain length.
0
 

Author Comment

by:tango2009
Comment Utility
minhvc

How can I remove those that begin with E?
0
 
LVL 18

Accepted Solution

by:
Cluskitt earned 500 total points
Comment Utility
Well, adjusting to C# (I'm a VB guy) it would be something like:

foreach (string s in SplitString)
{
  if (s.Length>50)
  {
    --Code here for over a value
  }
  if (s.SubString(0, 1)='E')
  {
    --Code here for starts with E
  }
}

Something like this anyway
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:tango2009
Comment Utility
Thanks for that, the only problem I have is putting them into separate strings. Using your code this is what I have below in the code section. When it iterates through it of course replaces the first number I want with the second. How do I separate them so one string will take the first number I want and a second string will take the second.
string[] digits = UniqueCode.Split(',');

                

                foreach (string s in digits)
               {
                 if (s.Length>5)
               {
                   HUCode = s;
                     
                  
               }
                 if (s.Substring(0,1).Contains("E"))
               {
                   s.Remove(0,5);
                  
               }
             }

Open in new window

0
 
LVL 18

Expert Comment

by:Cluskitt
Comment Utility
Not sure if I understand exactly what you want. If you want to split codes to one array and numbers to another, you can try something like:

string[] digits = UniqueCode.Split(',');
string[] Codes = new string[digits.Length - 1];
string[] Numbers = new string[digits.Length - 1];
int a,b;
a=0;
b=0;
                foreach (string s in digits)
               {
                 if (s.Length>5)
               {
                   Numbers[a]=s;
                  a +=1;
               }
                 if (s.Substring(0,1).Contains("E"))
               {
                   Codes[b]=s;
                  b +=1;
               }
             }

Open in new window

0
 

Author Comment

by:tango2009
Comment Utility
What I want to do is to get these two numbers from the string

94399960 94401460

assign the first number to one string and the second number to another string.

so the result should be

string one = 94399960
string two = 94401460
0
 
LVL 18

Expert Comment

by:Cluskitt
Comment Utility
You can split them again using ' '.
0
 

Author Comment

by:tango2009
Comment Utility
Oh of course you can, sorry stupid question.
0
 
LVL 18

Expert Comment

by:Cluskitt
Comment Utility
There are no stupid questions. As long as you learn, it's all good. You can also try breaking the string with substring and indexof(' ',0). But I prefer splitting.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now