switch statemeent. Am I missing someting
-
Way easier way to do that using the latest version of C#:
string input = "This is a c# program!";
var newChars = from character in input
where !char.IsPunctuation(character) && !char.IsWhiteSpace(character)
select char.ToUpper(character);string result = new string(newChars.ToArray()); // results in "THISISACPROGRAM"
Life, family, faith: Give me a visit. From my latest post: "A lot of Christians struggle, perhaps at a subconscious level, about the phrase "God of Israel". After all, Israel's God is the God of Judaism, is He not? And the God of Christianity is not the God of Judaism, right?" Judah Himango
Thanks for that. As you said, the code is for the latest version of C# and I am using 2005. I came up with this that seems to do the job
public static string CVNameSearch(string inputStr)
{
int x;
string returnStr;returnStr = ""; for (x = 0; x<= inputStr.Length -1; ++x) { if (char.IsLetter(inputStr, x)) { returnStr += inputStr.Substring(x, 1).ToUpper(); } } return returnStr; }
Thanks for your help today
The FoZ
-
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
-
Why not something simple, like this?
str = Regex.Replace(str, "[^A-Za-z]+", string.Empty).ToUpper();
Despite everything, the person most likely to be fooling you next is yourself.
-
Nice cheers. Don't suppose you could explain the syntax for me. I gather from the help that
str
is the input string,"[^A-Za-z]+"
is the match but I'm not sure how it works. What is thestring.Empty
for? Thanks for your helpThe FoZ
[^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. -
Nice cheers. Don't suppose you could explain the syntax for me. I gather from the help that
str
is the input string,"[^A-Za-z]+"
is the match but I'm not sure how it works. What is thestring.Empty
for? Thanks for your helpThe FoZ
The pattern "[^A-Za-z]+" matches any non-letter characters, which then are replaced by an empty string, so that any non-letter characters are removed from the string. The ^ in the set makes it a negative set, mathing any characters except A-Z and a-z. If you want to keep any other characters, you just add them in the pattern, like for example "[^A-Za-zÅåÄäÖöÉéÈèËëÑñ]+".
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:
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
-
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:
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
-
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.