switch statemeent. Am I missing someting
-
[^A-Za-z]+
Basically this is a regular expression pattern which matches:[^]+
==> any character that is not included inside the bracketsA-Z
==> matches any character in the range A to Za-z
==> matches any character in the range a to z So basically it replaces an non-alphabetic character withstring.Empty
.string.Empty
(orString.Empty
orSystem.String.Empty
) are essentially the same as""
however when you reference them it does not create a new object but utilises one that's already been created, thus more memory efficient. A small difference but sometimes it matters.Ed.Poore wrote:
string.Empty (or String.Empty or System.String.Empty) are essentially the same as "" however when you reference them it does not create a new object but utilises one that's already been created, thus more memory efficient. A small difference but sometimes it matters.
The performance difference is really minimal, but there is another difference. If you always use string.Empty when you want an empty string, the code gets clearer. If you happen to stumble upon a "" in the code, you know that there is supposed to be something between the quotation marks, that perhaps got erased or left out by mistake. :)
Despite everything, the person most likely to be fooling you next is yourself.
-
[^A-Za-z]+
Basically this is a regular expression pattern which matches:[^]+
==> any character that is not included inside the bracketsA-Z
==> matches any character in the range A to Za-z
==> matches any character in the range a to z So basically it replaces an non-alphabetic character withstring.Empty
.string.Empty
(orString.Empty
orSystem.String.Empty
) are essentially the same as""
however when you reference them it does not create a new object but utilises one that's already been created, thus more memory efficient. A small difference but sometimes it matters.Ed.Poore wrote:
does not create a new object but utilises one that's already been created
But in either case it stores an empty string in the executable at that point, because it's a constant, right? If so, there's no difference.
-
Thanks for the response. Do you have an example at all. What the function basically does is convert a string to an Upper Case string without any punctuation. e.g "This is a c# program!" converts to "THISISACPROGRAM" Many thanks
The FoZ
In C# you would use a bunch of if(c == foo) .... else if(30 < c && c < 100 && c == 10) ... etc. Of course with your particular issue doing it character by character probably isnt the best solution. I'd avoid using a Regex for something so simple. The framework has a bunch of stuff here thats probably useful (and culture invariant), eg: "foo bar".ToUpper().Replace(" ","") or similar. And who suggested LINQ? Just because LINQ is cool doesnt mean its appropriate to use it ;)
Mark Churchill Director Dunn & Churchill Free Download:
Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio. -
Ed.Poore wrote:
string.Empty (or String.Empty or System.String.Empty) are essentially the same as "" however when you reference them it does not create a new object but utilises one that's already been created, thus more memory efficient. A small difference but sometimes it matters.
The performance difference is really minimal, but there is another difference. If you always use string.Empty when you want an empty string, the code gets clearer. If you happen to stumble upon a "" in the code, you know that there is supposed to be something between the quotation marks, that perhaps got erased or left out by mistake. :)
Despite everything, the person most likely to be fooling you next is yourself.
-
Ed.Poore wrote:
does not create a new object but utilises one that's already been created
But in either case it stores an empty string in the executable at that point, because it's a constant, right? If so, there's no difference.
No it doesn't, I just compiled a sample and took a look at it under reflector and they definitely use two different methods, one compares a constant, the other compares the reference:
.entrypoint
.maxstack 2
.locals init (
[0] string toCompare,
[1] bool c1,
[2] bool c2)
L_0000: nop
L_0001: ldstr "Hello"
L_0006: stloc.0
L_0007: ldloc.0
L_0008: ldsfld string [mscorlib]System.String::Empty
L_000d: call bool [mscorlib]System.String::op_Equality(string, string)
L_0012: stloc.1
L_0013: ldloc.0
L_0014: ldstr ""
L_0019: call bool [mscorlib]System.String::op_Equality(string, string)
L_001e: stloc.2
L_001f: ret -
[^A-Za-z]+
Basically this is a regular expression pattern which matches:[^]+
==> any character that is not included inside the bracketsA-Z
==> matches any character in the range A to Za-z
==> matches any character in the range a to z So basically it replaces an non-alphabetic character withstring.Empty
.string.Empty
(orString.Empty
orSystem.String.Empty
) are essentially the same as""
however when you reference them it does not create a new object but utilises one that's already been created, thus more memory efficient. A small difference but sometimes it matters. -
Thanks for the info. 'Every little helps' when it all comes down to it. The Regex expression does exactly what the old VB code does in about 10 lines and lots of iterations through a loop.
The FoZ
-
Ed.Poore wrote:
string.Empty (or String.Empty or System.String.Empty) are essentially the same as "" however when you reference them it does not create a new object but utilises one that's already been created, thus more memory efficient. A small difference but sometimes it matters.
The performance difference is really minimal, but there is another difference. If you always use string.Empty when you want an empty string, the code gets clearer. If you happen to stumble upon a "" in the code, you know that there is supposed to be something between the quotation marks, that perhaps got erased or left out by mistake. :)
Despite everything, the person most likely to be fooling you next is yourself.
Excellent reasons. Another benefit is that it ensures the comparisons are always performed the same way...no more some tests looking for length == 0 while others looking for "".
Scott Dorman
Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
-
That's the best justification I've heard of it, thanks! I do use it but had always been slightly confused as to what the point was, now there's a valid reason.
Another benefit is that it ensures the comparisons are always performed the same way...no more some tests looking for length == 0 while others looking for "".
Scott Dorman
Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
-
Ed.Poore wrote:
does not create a new object but utilises one that's already been created
But in either case it stores an empty string in the executable at that point, because it's a constant, right? If so, there's no difference.
PIEBALDconsult wrote:
But in either case it stores an empty string in the executable at that point, because it's a constant, right? If so, there's no difference.
No, the string.Empty property returns a string that already exists in the mscorlib.dll. If you use "", that literal string will be added to your assembly. The difference is minimal, as it's only a few bytes of data, but there is a difference.
Despite everything, the person most likely to be fooling you next is yourself.