Solved

Split String in C#

Posted on 2014-01-08
4
310 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
ID: 39766406
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
ID: 39766420
too easy. Thanks
0
 
LVL 19

Expert Comment

by:Ken Butters
ID: 39766427
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
ID: 39766453
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

910 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

19 Experts available now in Live!

Get 1:1 Help Now