switch statemeent. Am I missing someting
-
Hi Please accept my apologies if this sound silly but I cannot find a solution to this. I have a project I am converting to C# from VB. In VB I would do this.
Select Case Asc(UCase(Mid(strName, intPos, 1)))
Case 48 To 57
Let strNameSearch = strNameSearch & UCase(Mid(strName, intPos, 1))
Case 65 To 90, 193, 201, 205, 211, 218, 225, 233, 237, 243, 250
'some other code here
End SelectI have looked on the interweb and the only solution I have found is to use the switch statement like this
switch (statement that returns the ASCII value of the text)
{
case 48:
case 49:
case 50:
//and so on
}This does seem a bit convoluted so is there an easy way like with the
To
statement in vb? (a link to some advanced switching will be greatly received) How do I get the ASC value of a character. Alot of people are saying to doConvert.ToInt32(....)
but this brings me back errors as I am using a for loop to iterate through asubstring
. Your help is greatly appreciated.The FoZ
-
Hi Please accept my apologies if this sound silly but I cannot find a solution to this. I have a project I am converting to C# from VB. In VB I would do this.
Select Case Asc(UCase(Mid(strName, intPos, 1)))
Case 48 To 57
Let strNameSearch = strNameSearch & UCase(Mid(strName, intPos, 1))
Case 65 To 90, 193, 201, 205, 211, 218, 225, 233, 237, 243, 250
'some other code here
End SelectI have looked on the interweb and the only solution I have found is to use the switch statement like this
switch (statement that returns the ASCII value of the text)
{
case 48:
case 49:
case 50:
//and so on
}This does seem a bit convoluted so is there an easy way like with the
To
statement in vb? (a link to some advanced switching will be greatly received) How do I get the ASC value of a character. Alot of people are saying to doConvert.ToInt32(....)
but this brings me back errors as I am using a for loop to iterate through asubstring
. Your help is greatly appreciated.The FoZ
Yes, there is no C# equivalent of VB's
Case 48 To 57
Best you can do inside a switch statement is case 48: case 49: case 50: etc. But that sounds really ugly. (In fact, the VB solution sounds like an ugly hackery.) A better option may be to utilize a table (such as a
Dictionary<int, string>
) to map integer keys to string values or functions that produce string values. Tell us what you want to do, and we'll tell you a good way to do it.TheFoZ wrote:
Alot of people are saying to do Convert.ToInt32(....) but this brings me back errors as I am using a for loop to iterate through a substring.
char someChar = 'A';
int charValue = (int)someChar; // should be 65Life, 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
-
Yes, there is no C# equivalent of VB's
Case 48 To 57
Best you can do inside a switch statement is case 48: case 49: case 50: etc. But that sounds really ugly. (In fact, the VB solution sounds like an ugly hackery.) A better option may be to utilize a table (such as a
Dictionary<int, string>
) to map integer keys to string values or functions that produce string values. Tell us what you want to do, and we'll tell you a good way to do it.TheFoZ wrote:
Alot of people are saying to do Convert.ToInt32(....) but this brings me back errors as I am using a for loop to iterate through a substring.
char someChar = 'A';
int charValue = (int)someChar; // should be 65Life, 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 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
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
-
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
-
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.