Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Split String in C#

Posted on 2014-01-08
4
Medium Priority
?
335 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 2000 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

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…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

598 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