tags:

views:

190

answers:

4

Sorry if that question has no answer.. What is the easiest and non-verbose way to make all the strings in C# application trimmed by default? Creating new Type, or extension method, or Attribute? Are there any hacks or settings to decrease number of Trim() calls?

UPD. Well, I have no particular case - just a lot of user input from UI or Excel or something else, and you should always keep in mind and Trim(), Trim(), Trim(). I really wonder why strings are not trimmed by default in C#.

A: 

Kinda hard to tell from the question; but I would probably create a custom textbox that overrides the Text method with Text.Trim()

Alternatively, you could create a method in a common class that would Trim whatever is passed into it. So before using any strings; you'd run them through that method first.

If you could elaborate a bit on your original question, that would help

Jim B
Why do you assume the value is coming from a TextBox control? The OP doesn't even mention a UI.
James
@Jim: "create a method in a common class that would Trim whatever is passed into it". Do you mean something like the `Trim` method in the common class `System.String`? ;)
LukeH
James: It was just a guess; the OP really didn't give much to go on. I figure if you're trimming stings, they're probably coming from user input.Luke: Exactly; instead of having String.Trim() all over the place; create one method that calls String.Trim() and then pass your strings through that
Jim B
@Jim: But something like `MyClass.MyMethod(myString)` or `myString.MyExtensionMethod()` doesn't offer any benefit over using plain `myString.Trim()`, does it?
LukeH
The only advantage that method has would be to avoid peppering the code with .Trim()'s everywhere.It could also be beneficial in terms of scalability; suppose going forward all carriage returns need to be parsed out, making one change to that method would eliminate having to find all the usages of .Trim() scattered through the code
Jim B
A: 

If you mean strings that you are getting for a resource file or something, then just create a utility function that gets the strings, and call Trim() in there, so you never need to worry about it in calling code.

Fiona Holder
A: 

If find yourself using the value in the same scope a lot then to reduce the number of times you call Trim() the best option would be trim the value once and store it as a variable i.e.

var trimmedStr = myStr.Trim();

An extension method would be pointless as it is pretty much the equivalent of calling Trim() as I imagine you would just be doing this internally. Creating a custom type just to ensure your string has no white space is a bit over-engineering and unnecessary.

James
+1  A: 

There are plenty of drawbacks, but you could do something like:

    class StringT
    {
        private String _s;
        public StringT(String v)
        {
            if (!String.IsNullOrEmpty(v)) { _s = v.Trim(); }
              else { _s = v; }
        }

         public static implicit operator StringT(String v) { return new StringT(v); }

         public static implicit operator String(StringT v) { return v._s; }
    }
Jon.Stromer.Galley