Solved

Extracting integers from comma separated string

Posted on 2011-09-06
11
382 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
ID: 36487480
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
ID: 36487492
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
ID: 36487497
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:tango2009
ID: 36487503
minhvc

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

Accepted Solution

by:
Cluskitt earned 500 total points
ID: 36487521
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
 

Author Comment

by:tango2009
ID: 36487597
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
ID: 36487643
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
ID: 36487651
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
ID: 36487667
You can split them again using ' '.
0
 

Author Comment

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

Expert Comment

by:Cluskitt
ID: 36487724
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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…

840 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