8

I have an input string as follows:

thumb_634735515600845357tchayat_november_200612.jpg

What I want to do is first split this string by _. And then take the resulting tokens at positions 1 to n and join them.

Specifically, with respect to my sample input, here is my desired output. As you can see, thumb_ has been removed from the front of the string:

634735515600845357tchayat_november_200612.jpg

I know how to do the split. But then how do I do the join step that follows? I do realize that I can use a for loop to do the join. But is there a better way? I can't use a sub-string approach to do the join step because I have data before thumb_.

Finally, note that the _ character that follows thumb is always the first instance of _.

DavidRR
  • 18,291
  • 25
  • 109
  • 191
DarthVader
  • 52,984
  • 76
  • 209
  • 300

6 Answers6

12

Since the _ after the thumb is always the first instance then Substring should be perfectly viable in conjunction with IndexOf.

string newString = myString.Substring(myString.IndexOf("_") + 1);

This should take the substring starting at the character immediately following the first instance of the _ character.

If you wish to get the second index of _, here's what I recommend:

int first = myString.IndexOf("_");
int second = mystring.Substring(first).IndexOf("_");

See this answer by Jon Skeet to the question "Index of the nth occurrence of a string?" for support for this approach.

Community
  • 1
  • 1
Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
  • Since C#8 and the introduction of Range Structs, the recommended format for the first line of code would now be myString[(myString.IndexOf("_") + 1)..] – Haighstrom Apr 11 '22 at 14:57
7

If you don't like indexOf, but want Join instead:

String.Join("_",
   "thumb_634735515600845357tchayat_november_200612.jpg"
   .Split('_')
   .Skip(1)
   .ToArray())
DarthVader
  • 52,984
  • 76
  • 209
  • 300
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
2

After splitting and rearranging you can use Join method.

string new_value=String.Join("",your_array);
Md Kamruzzaman Sarker
  • 2,387
  • 3
  • 22
  • 38
0

You can use String.Join, but actually, it still use a loop inside.

String.Join("_", "thumb_634735515600845357tchayat_november_200612.jpg".Split('_').Skip(1).ToArray());
Kirin Yao
  • 1,606
  • 2
  • 14
  • 21
0

UserRoles are the list of strings:

string.Join(",", userRoles.ToArray());
Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
Mohamed.Abdo
  • 2,054
  • 1
  • 19
  • 12
0

Original post was on Aug. 31, 2015. The only thing that I've changed is the markup syntax because the original was wrong.

The use of regular expressions can be very helpful.

using System.Text.RegularExpressions;

string itemName = "thumb_634735515600845357tchayat_november_200612.jpg";
itemName = Regex.Match(itemName, @"(?<=(_)).*$").Value;

The "(?<=(_))" looks behind the first "_". Think of it as a starting point. The ".*$" gets everything from the starting point to the end of the line. "$" marks the end of the line.

T.CK
  • 411
  • 4
  • 6