Click here to Skip to main content
15,945,119 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I find second occurrences "-" in string and split
Example:
myFileName-12345678-Inbound-Test_ND.txt
I need to get only:myFileName-12345678
Posted

Use

string[] tokens = originalFileName.Split(new char[] {'-', '.'});


After that, re-compose the output string fragment at your liking. Prefer string.Format over string concatenation, for example:

int tokenLength = tokens.Length;
if (tokenLength > 2)
   myNewFileName = string.Format("{0}-{1}.{2}",
        tokens[0], tokens[1], tokens[tokenLength - 1]);


—SA
 
Share this answer
 
v3
You can take the substring using Substring method and define the starting point based on the value returned by IndexOf('-').
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900