Solved

Split String in C#

Posted on 2014-01-08
4
308 Views
Last Modified: 2014-01-08
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
0
Comment
Question by:BKennedy2008
4 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
this should show how to get it:
string _filename  = "JOBNO_2014-01-0127_154156_URI-Supply-BLANK-77.xlsm";
string[] _parts = _filename.Split('_');
string _jobno = _parts[1]; 

Open in new window

0
 

Author Comment

by:BKennedy2008
Comment Utility
too easy. Thanks
0
 
LVL 19

Expert Comment

by:Ken Butters
Comment Utility
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.
0
 
LVL 29

Expert Comment

by:anarki_jimbel
Comment Utility
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

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 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

11 Experts available now in Live!

Get 1:1 Help Now