tags:

views:

76

answers:

2

Here is the code:

namespace TrimTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string ToTrim = "PRN.NUL";
            Console.WriteLine(ToTrim);
            string Trimmed = ToTrim.TrimStart("PRN.".ToCharArray());
            Console.WriteLine(Trimmed);
            ToTrim = "PRN.AUX";
            Console.WriteLine(ToTrim);
            Trimmed = ToTrim.TrimStart("PRN.".ToCharArray());
            Console.WriteLine(Trimmed);
            ToTrim = "AUX.NUL";
            Console.WriteLine(ToTrim);
            Trimmed = ToTrim.TrimStart("AUX.".ToCharArray());
            Console.WriteLine(Trimmed);
        }
    }
}

The output is like this:

PRN.NUL

UL

PRN.AUX

AUX

AUX.NUL

NUL

As you can see, the TrimStart took out the N from NUL. But it doesn't do that for other strings even if it started with PRN.

I tried with .NET Framework 3.5 and 4.0 and the results are same. Are there any explanation on what causes this behavior?

+1  A: 

Try this:

string ToTrim = "PRN.NUL";
string Trimmed = ToTrim.TrimStart(".NRP".ToCharArray());
Console.WriteLine(Trimmed);

Notice anything?

Will
His face will probably look like your icon :-o
SwDevMan81
@swd lol probably
Will
+3  A: 

String.TrimStart works on a character level. What you're doing is you're telling it to remove any "P", "R", "N" or "." characters from the start - therefore the first N after the dot also gets removed.

If you want to remove a certain string from the start of the string, first use StartsWith to ensure it's there and then Substring to take the correct part of the string.

Matti Virkkunen
Aw, you didn't have to spell it out for him.
Will
At least I didn't give him copy-pasteable code (not referring to your code, some people on SO just have a nasty habit of giving copy-paste ready fixes (I have to admit I've been guilty of this a few times too))
Matti Virkkunen
@matti It may have been copypasteable, but it wasn't the answer. It was code that demonstrated the same result but in a way where he could understand what was happening. Take a look at it again. Same char array, different order.
Will
I will come back to why I asked later. But the answer came too soon.
James
@Will: I revised my comment to avoid confusion right after I posted it, I guess you saw the old version...
Matti Virkkunen
@matti: that is a two step process. I prefer .TrimStart("PRN"). Now I have code all over doing if (x.StartsWith(somestring)) y = x.Remove(0, somestring.Length); Lots of them still in Framework 1.1.
James
@James: And what do programmers do when they don't want to repeat something all the time? They make a method out of it! How about you make a method out of that as well. If you can't use extension methods due to still living in the previous decade or something, just throw it in a static class.
Matti Virkkunen
@matti: programmers ignore the local methods when they are dealing with lots of assemblies. still trying to get out of previous decade. few hundred thousand lines of code are not magically converting to latest framework.
James
@James: Actually there *are* automagical tools that do most of the work... Also, if you just found out that TrimStart doesn't work the way you expected it to, how do you already have the "two-step fix" all over the place? You couldn't have replaced stuff *that* fast. Just go in and change all those ifs to a static method call.
Matti Virkkunen
@matti: tried some tools. may not have been the best, way too many obsolete methods, third party controls that requires more than just relinking with their latest assembly and so on. Two step code already exists everywhere in the old framework code. For new code in 3.5 I was looking for a single step, and was kind of disappointed.
James
@James: If you can use 3.5, by all means make this an extension method. Then you can call `.RemovePrefix` (or whatever you want to call it) on any string.
Matti Virkkunen
@matti: Sure, but the challenge is in getting everyone else use all those little methods that doesn't show up in intellisense.
James
@James: Actually, extension methods do show up in IntelliSense, as long as you import the namespace they're on.
Matti Virkkunen
@matti oops, I didn't pay attention to the "extension" part. yes, that will do. just need to make sure to place them inside a namespace that is commonly used. thanks.
James