• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 388
  • Last Modified:

Extracting integers from comma separated string

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
tango2009
Asked:
tango2009
  • 5
  • 5
1 Solution
 
CluskittCommented:
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
 
Minh Võ CôngCommented:
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
 
tango2009Author Commented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
tango2009Author Commented:
minhvc

How can I remove those that begin with E?
0
 
CluskittCommented:
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
 
tango2009Author Commented:
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
 
CluskittCommented:
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
 
tango2009Author Commented:
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
 
CluskittCommented:
You can split them again using ' '.
0
 
tango2009Author Commented:
Oh of course you can, sorry stupid question.
0
 
CluskittCommented:
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now