Solved

Split String in C#

Posted on 2014-01-08
4
327 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 143

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 30

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

Technology Partners: 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!

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

728 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