tags:

views:

52

answers:

3

I was using the TrimStart function to do the following:

var example = "Savings:Save 20% on this stuff";
example = example.TrimStart("Savings:".ToCharArray());

I was expecting this to result in example having a value of "Save 20% on this stuff".

However, what I got was "e 20% on this stuff".

After reading the documentation on TrimStart I understand why, but now I'm left wondering if there is a function in .NET that does what I was trying to do in the first place?

Does anyone know of a function so I don't have to create my own and keep track of it?

+10  A: 

I don't think such a method exists but you can easily do it using StartsWith and Substring:

s = s.StartsWith(toRemove) ? s.Substring(toRemove.Length) : s;

You can even add it as an extension method:

public static class StringExtension
{
    public static string RemoveFromStart(this string s, string toRemove)
    {
        if (s == null)
        {
            throw new ArgumentNullException("s");
        }

        if (toRemove == null)
        {
            throw new ArgumentNullException("toRemove");
        }

        if (!s.StartsWith(toRemove))
        {
            return s;
        }

        return s.Substring(toRemove.Length);
    }
}
Mark Byers
@Mark thanks for the extension. I was trying to avoid writing my own code to do this, but I guess it's unavoidable.
Joseph
I'd personally go with a conditional here: `return s.StartsWith(toRemove) ? s.Substring(toRemove.Length) : s;` But I know not everyone is a fan of the conditional operator :)
Jon Skeet
This is definitely the way to go, clear and easy to verify correctness. Here's an horrifically evil alternative I've actually seen in production code (altered to fit the example): `example.Split(new[] {"Savings:"}, 2, StringSplitOptions.RemoveEmptyEntries)[0]`. I don't know what the original author was thinking...
LBushkin
+2  A: 

No, I don't believe there's anything which does this built into the framework. It's a somewhat unusual requirement, IMO.

Note that you should think carefully about whether you're trying to remove "the first occurrence" or remove the occurrence at the start of the string, if there is one. For example, think what you'd want to do with: "Hello. Savings: Save 20% on this stuff".

Jon Skeet
@Jon Good Point.. My intention was only at the start of the string, not actually the first occurence. But again, only the first time, not repeatedly.
Joseph
@Joseph: In that case, Mark's answer should do you fine. I'll leave this one here to make it clear.
Jon Skeet
+2  A: 

You can do that quite easily using a regular expression.

Remove the occurence on the beginning of the string:

exmaple = Regex.Replace(example, @"^Savings:", "");

Remove the first occurence in the string:

exmaple = Regex.Replace(example, @"(?<!Savings:.*)Savings:", "");
Lucero
That doesn't look as simple to me as having a single extra (simple) method. In particular, it gets hairy as soon as you need to start removing something that has special meaning in regular expressions.
Jon Skeet
@Jon, not at all, build the string using `Regex.Escape()` on the text to match and you're good.
Lucero
@Lucero: Yes, but you've got to remember to do that... which I suspect many people wouldn't. I think Mark's solution is a lot simpler. More lines of code, but harder to get wrong.
Jon Skeet
@Jon, I agree with that. However, as with many other things (such as with LINQ's deferred execution), it's always a matter of knowing what you're doing to get it right.
Lucero