[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 368
  • Last Modified:

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?

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

string firstVariable = words[0];
string secondVariable = words[0];
0
 
DhaestCommented:
Correction :)

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

string firstVariable = words[0];
string secondVariable = words[1];
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
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!

 
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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