Solved

Extracting integers from comma separated string

Posted on 2011-09-06
11
380 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

920 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

18 Experts available now in Live!

Get 1:1 Help Now