Click here to Skip to main content
14,359,798 members
Rate this:
Please Sign up or sign in to vote.
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
Rate this:
Please Sign up or sign in to vote.

Solution 1

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
   
v3
Rate this:
Please Sign up or sign in to vote.

Solution 2

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

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100