Avatar of BKennedy2008
BKennedy2008
 asked on

Split String in C#

I have a string = JOBNO_2014-01-0127_154156_URI-Supply-BLANK-77.xlsm
or it can equal
JOBNO_2014-01-0127-CON_154156_URI-Supply-BLANK-77.xlsm

The Job Number can vary in length, but always will be the second part of the string seperated by  an underscore.

I need to do a split string and pull out  2014-01-0127 or 2014-01-0127-CON
in a public void for C# and assign it to a variable.

Can someone assist me. Thanks
C#

Avatar of undefined
Last Comment
Dmitry G

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Guy Hengel [angelIII / a3]

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
BKennedy2008

ASKER
too easy. Thanks
Ken Butters

Supposing variable s contains string :  JOBNO_2014-01-0127_154156_URI-Supply-BLANK-77.xlsm

Executing : string[] words = s.Split('_');

Then : words[0] should contain your desired string.
Dmitry G

One more way:

            string test = "JOBNO_2014-01-0127-CON_154156_URI-Supply-BLANK-77.xlsm";
            int underscoreIndex1 = test.IndexOf('_');
            int underscoreIndex2 = test.IndexOf('_',  underscoreIndex1+1 );
            string result = test.Substring(underscoreIndex1+1, underscoreIndex2 - underscoreIndex1-1);
            System.Diagnostics.Debug.WriteLine(result);

Open in new window

Your help has saved me hundreds of hours of internet surfing.
fblack61