Split string C#

Hi,

I have the following string:  "50_100_5TH RED_PAR / DAR_TB" (which is a variable)

I would like to get the "50" and the "100" into 2 seperate strings.
Note that the seperators are "-" underscores

Any idea's?

DeveloperUserAsked:
Who is Participating?
 
DhaestConnect With a Mentor Commented:
Correction :)

string s = "50_100_5TH RED_PAR / DAR_TB";
string[] words = s.Split('_');

string firstVariable = words[0];
string secondVariable = words[1];
0
 
DhaestCommented:
string s = "50_100_5TH RED_PAR / DAR_TB";
string[] words = s.Split('_');

string firstVariable = words[0];
string secondVariable = words[0];
0
 
Gene_CypCommented:
I'm not posting to get points, just adding some comment to explain what dhaest gave you:

"50_100_5TH RED_PAR / DAR_TB"

will be split into:

50
100
5TH Red
PAR/DAR
TB

with positions in the array Words: 0-4

So, in the future when you need to do something like that again you'll know how to adapt it.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
kambleamarCommented:
if they are only numbers you are trying to grab ,
execute the upper bound of words splitted array and
you can try cross  checking them as
CINT ( words[1]) , if they are integer the u can grab them
Amar
0
 
anarki_jimbelCommented:
Dhaest solution works perfectly. But it allocates 5 strings :)

My code allocates two strings only - but more calculus is involved :)


string s = "50_100_5TH RED_PAR / DAR_TB";
            string firstVariable = s.Substring(0,s.IndexOf("_"));
            string secondVariable = s.Substring(firstVariable.Length + 1, s.IndexOf("_", firstVariable.Length+1) - firstVariable.Length - 1);
            MessageBox.Show(firstVariable + Environment.NewLine + secondVariable);

Open in new window

0
 
kambleamarCommented:
what do you want do exactly, can you please be precise
Amar
0
 
DeveloperUserAuthor Commented:
Perfect!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.