3

I have a string which I would like to remove the first three characters from. How do I go about this using substrings, or is there any other way?

string temp = "01_Barnsley"
string textIWant = "Barnsley"

Thank you

Phil
  • 563
  • 4
  • 6
  • 16
  • 3
    We need to start tagging questions like these as `frist-psot` (a la Slashdot) given the typo encrusted race to answer it results in. Mash it out then enter the edit frenzy! – x0n Feb 20 '14 at 13:27
  • 1
    Are you sure you want to remove the first three characters? It seems to me that you're more interested in removing everything up to (and including) the first underscore. – Steven Liekens Feb 20 '14 at 13:31
  • That question is way too deep for SO. I mean, what _is_ Google? – Steven Liekens Feb 20 '14 at 13:34
  • Does this answer your question? [How to remove first 10 characters from a string?](https://stackoverflow.com/questions/7186648/how-to-remove-first-10-characters-from-a-string) – Heretic Monkey May 25 '21 at 13:27

4 Answers4

21

You can use String.Substring(Int32) method.

Retrieves a substring from this instance. The substring starts at a specified character position and continues to the end of the string.

string textIWant = temp.Substring(3);

Here is a demonstration.

As an alternative, you can use String.Remove method(Int32, Int32) method also.

Returns a new string in which a specified number of characters in the current instance beginning at a specified position have been deleted.

string textIWant = temp.Remove(0, 3);

Here is a demonstration.

j.wittwer
  • 9,497
  • 3
  • 30
  • 32
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
3

you can use String.Substring Method (Int32)

string textIWant = temp.Substring(3);

or String.Remove Method (Int32, Int32)

string textIWant = temp.Remove(0,3);
Nagaraj S
  • 13,316
  • 6
  • 32
  • 53
1

If there is a pattern to the data, one can use that to extract out what is needed using Regular Expressions.

So if one knows there are numbers (\d regex for digits and with 1 or more with a +) followed by an under bar; that is the pattern to exclude. Now we tell the parser what we want to capture by saying we want a group match using ( ) notation. Within that sub group we say capture everything by using .+. The period (.) means any character and the + as seen before means 1 or more.

The full match for the whole pattern (not what we want) is grouped as at index zero. We want the first subgroup match at index 1 which is our data.

Console.WriteLine (Regex.Match("01_Barnsley",   @"\d+_(.+)").Groups[1].Value); // Barnsley
Console.WriteLine (Regex.Match("9999_Omegaman", @"\d+_(.+)").Groups[1].Value); // Omegaman

Notice how we don't have to worry if its more than two digits? Whereas substring can fail because the number grew, it is not a problem for the regex parser due to the flexibility found in our pattern.

Summary

If there is a distinct pattern to the data, and the data may change, use regex. The minimal learning curve can pay off handsomely. If you truly just need something at a specific point that is unchanging, use substring.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
0

A solution using LINQ:

string temp = "01_Barnsley";
string textIWant = new string(temp.Skip(3).ToArray());
w.b
  • 11,026
  • 5
  • 30
  • 49