Scenario
Consider the following code snippet.
string s = "S";
string s1 = "S";
string s2 = string.Empty;
switch (s)
{
case "S":
s1 = "StringComparison";
break;
default:
break;
}
switch (s[0])
{
case'S':
s2 = "StringCOmpare2";
break;
default:
break;
}
the first switch case, results in a stringcomparison within IL.
But the second switch case, does not result in a stringcomparison within IL.
Can anyone justify this?
Because on the second switch you're are not doing a String comparison, you're doing a Char comparison.
The easiest answer is that you're not doing a string comparison in the second block; you're comparing two characters.
However, you're right in that the two code blocks are functionally equivalent. A good optimizing compiler should be able to detect that 's' is a fixed-length string, and rewrite it not to use a full string comparison.
You're accessing the string via its indexer which returns a char and so lets you use the string as if it was an array of chars.
So whar you're doing is a char comparison. Using the apostrophe for the 'S' also tells you that you're using 'S' as a char and not as a string.
Your second switch statement isn't using a string, but a single char. Hence, no string comparison.
Related
I have used below code in a project and someone ask me to use ToLower() or ToUpper() and I think it is unnecessary.
public somefun(Classabc clsabc, string emptype, string id)
{
switch(emptype)
{
case :"AnyInput":
{
//do
}
break;
case :"StringInput":
{
//do
}
break;
}
}
if(emptype=="AnyInput")
{
///
}
Is the above perfect or we need to use ToLower() or ToUpper() with empType in if()? Is there any issue or programming rule violation with my code? According to me in case (switch) we are using email type as constant for case matching and if emptype value can be used in case matching then there is no need to add extra functions to convert to there case before string matching.
Depends on what you guys are looking for.
If the comparison is case sensitive, you can keep the switch-case comparison like you did in the example you provided.
In case the comparison is insensitive, you can pattern matching (C# 7 and above) and write something like this:
switch (true)
{
case bool b when emptype.Equals("AnyInput", StringComparison.InvariantCultureIgnoreCase):
// do
break;
case bool b when emptype.Equals("StringInput", StringComparison.InvariantCultureIgnoreCase):
// do
break;
default:
break;
}
I am looking for a function that can check the character if it is a integer and do something is so.
char a = '1';
if (Function(a))
{
do something
}
Use System.Char.IsDigit method
If you want just the pure 0-9 digits, use
if(a>='0' && a<='9')
IsNumeric and IsDigit both return true for some characters outside the 0-9 range:
Difference between Char.IsDigit() and Char.IsNumber() in C#
Integer.TryParse works well.
http://msdn.microsoft.com/en-us/library/f02979c7.aspx
The bool Char.IsDigit(char c); Method should work perfectly for this instance.
char a = '1';
if (Char.IsDigit(a))
{
//do something
}
Try using System.Char.IsDigit method.
Try Char.IsNumber. Documentation and examples can be found here
It may be better to just use a switch statement. Something like:
switch(a)
{
case '1':
//do something.
break;
case '2':
// do something else.
break;
default: // Not an integer
throw new FormatException();
break;
}
This will work as long as you're only looking for characters 0-9. Anything more than that (say "10") would be a string and not a character. If you're trying to just see if some input is an integer and the input is a string, you can do:
try
{
Convert.ToInt32("10")
}
catch (FormatException err)
{
// Not an integer, display some error.
}
I have to check the first to characters of a string and if the third character is numeric and do it with MyString.All(char.IsDigit):
if (cAdresse.Trim().ToUpper().Substring(0, 2) == "FZ" & cAdresse.Trim().ToUpper().Substring(2, 1).All(char.IsDigit))
Simplest answer:
char chr = '1';
char.isDigit(chr)
Here if my usename is Rus7AE then i want to take the number value i.e. third character from right. t.e. 7. Following code is not returning me the value 7 ?
studentA consists of 5-7th std studentB consists of 8-10th std studentC consists of 5-10th std thats common group but doesnt contains same values from studentA and studentB
private void authcheck()
{
username = Session["stud"].ToString();
schlName = Session["stuschname"].ToString();
string value = username.ToString();
int groupName = Convert.ToInt32(value[value.Length - 3]);
Session["grpName"] = groupName.ToString();
}
How can i get the thirdlast value
You are trying to convert a character to a numeric value, which will not give the intended result.
Try
int groupName = int.Parse(value.Substring(value.Length - 3, 1));
If it is possible that you will receive data not in this format, look at int.TryParse instead.
Convert.ToInt32 is not working because it takes the actual value of the character based on the current encoding and converts that to a character. For example,
int val = Convert.ToInt32('7');
Yields 55, which is the ASCII or UTF-8 code for '7'.
You don't get third last value because the conditions are wrong
if
5-7
else if
8-10
else if
5-10
Last one will never be hit in this case
since you can have "10" in your group-number (as i've seen in your code) and maybe some dual-digits more (11,12,13 etc.) we should use Regular Expressions here...
var regex = new Regex(#"\w+(\d+)\w+");
int groupNumber;
var match = regex.Match(subjectString);
if(!match.Success || !match.Groups[1].Success || !int.TryParse(match.Groups[1].Value, out groupNumber))
throw new Exception("Student has no group!?!?");
string groupName = null;
switch(groupNumber){
case 5:
case 6:
case 7:
groupName = "studentA";
break;
case 8:
case 9:
case 10:
groupName = "studentB";
break;
case 1:
case 2:
case 3:
case 4:
groupName = "studentC";
break;
default:
throw new Exception("invalid group number");
}
I am trying to display an error to the user of a web page using a javascript alert popup, I currently have the following code to clean the error string:
errorMessage.Replace("'", "\'")
But this is not sufficient as some illegal characters are not being removed, is there a static method somewhere in the framework that will format my string for clean insertion into html?
Update: my initial question was slightly ambiguous.
the string needs to be valid as in alert('this is some 'illegal text' that will not popup');
I will try Server.HtmlEncode, hopefully it will do the trick.
If you have a look at the AntiXSS module in the Web Protection Library, you'll find that it has a JavaScriptEncode(string) method for just this sort of thing.
There's a simple solution...use the DataContractJsonSerializer and "serialize" the string value. By serializing the string to JSON, you're by definition ensuring that it'll work nicely inside an alert statement.
You want to avoid XSS vulnerabilities, which is good. The following cheat sheet should assist you (and also contains a reference to code for escaping the string):
http://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet
To escape a string for clean insertion into html you can use HttpUtility.HtmlEncode method. I'm not sure it this helps you with the javascript.
http://msdn.microsoft.com/en-us/library/73z22y6h.aspx
If you are using ASP.NET 4 you can use the new <%: %> syntax to HtmlEncode the generated string AND replace the default encoder with AntiXSS, or any other library you may prefer. Phil Haack explains how to do this in Using AntiXss as the default encoder for ASP.NET
This way you can write an alert like this:
alert('<%: this.ErrorMessage %>');
In previous versions you can use what others have suggested, either HtmlEncode or AntiXss like this:
alert('<%= HttpUtility.HtmlEncode(this.ErrorMessage) %>');
or
alert('<%= AntiXss.HtmlEncode(this.ErrorMessage) %>');
Thanks for the help but none of the answers presented gave me the complete solution.
Joe's answer was closest but it did not account for \r\n newline. I could not find a way to translate c# newline into a javascript equivalient.
public static string EscapeAlertMessage(string value)
{
value = value.Replace("\\", "\\\\");
value = value.Replace("'", "\\'");
value = value.Replace("\"", "\\\"");
value = value.Replace(Environment.NewLine, "--");
return value;
}
HttpUtility.HtmlEncode will not encode a single quote (') and is not suitable for encoding a string to be used as an alert message. Personally I do it like this:
public static string EscapeAlertMessage(string value)
{
value = value.Replace("\\", "\\\\");
value = value.Replace("'", "\\'");
value = value.Replace("\"", "\\\"");
return value;
}
If your message contains multiple lines, you can replace them by "\n" - e.g. if the lines are separated by Environment.NewLine:
value = value.Replace(Environment.NewLine, "\\n");
Or if you don't know what the separators are (\r\n, \n or \r only) you could use:
value = value.Replace("\r", "\\r");
value = value.Replace("\n", "\\n");
I use the following function in my projects. It escpaes all possible characters for Javascript:
/// <summary>
/// Encodes a string to be represented as a string literal. The format
/// is essentially a JSON string.
///
/// Example Output: Hello \"Rick\"!\r\nRock on
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string EncodeJsString(string s)
{
StringBuilder sb = new StringBuilder();
foreach (char c in s)
{
switch (c)
{
case '\"':
sb.Append("\\\"");
break;
case '\\':
sb.Append("\\\\");
break;
case '\b':
sb.Append("\\b");
break;
case '\f':
sb.Append("\\f");
break;
case '\n':
sb.Append("\\n");
break;
case '\r':
sb.Append("\\r");
break;
case '\t':
sb.Append("\\t");
break;
case '\'':
sb.Append("\\'");
break;
default:
int i = (int)c;
if (i < 32 || i > 127)
{
sb.AppendFormat("\\u{0:X04}", i);
}
else
{
sb.Append(c);
}
break;
}
}
return sb.ToString();
}
Is it possible to have a switch in C# which checks if the value is null or empty not "" but String.Empty? I know i can do this:
switch (text)
{
case null:
case "":
break;
}
Is there something better, because I don't want to have a large list of IF statements?
I'mm trying to replace:
if (String.IsNullOrEmpty(text))
blah;
else if (text = "hi")
blah
I would suggest something like the following:
switch(text ?? String.Empty)
{
case "":
break;
case "hi":
break;
}
Is that what you are looking for?
What's wrong with your example switch statement?
switch (text)
{
case null:
case "":
foo();
break;
case "hi":
bar();
break;
}
It works (and for some reason that surprised me - I thought it would complain or crash on the null case) and it's clear.
For that matter, why are you worried about String.Empty? I'm missing something here.
how about
if (string.isNullOrEmpty(text))
{
//blah
}
else
{
switch (text)
{
case "hi":
}
}
From the documentation of String.Empty:
The value of this field is the
zero-length string, "".
I interpret this to mean that there is no difference between "" and String.Empty. Why are you trying to distinguish between them?
An empty string is "", which is equal to String.Empty. The reason that you can put "" in a case statement but not "String.Empty" is that "Empty" is a field of the class "String" and "" is actually a contant value.
Constant values are allowed in cases, String.Empty is a field and could be altered at run time. (In this case it will remain the same, but not all static fields of each class are constant values.)
In the case of 'if', that condition is evaluated at run time and if does not require a constant value.
I hope this explains why.
Something that I just noticed is that you can combine if/else and switch statements! Very useful when needing to check preconditions.
if (string.IsNullOrEmpty(text))
{
//blah
}
else switch (text)
{
case "hi":
Console.WriteLine("How about a nice game of chess?");
break;
default:
break;
}
With new c# features, you can use switch expression syntax
text switch
{
"" or null => "a",
_ => "b"
};
string StrMode;
if (!string.IsNullOrEmpty(StrMode))
{
switch (StrMode.Trim())
{
case "Souse":
{
//Statement Eg:
MesssageBox.Show("Souse");
break;
}
case "Company Agent":
{
//Statement Eg:
MesssageBox.Show("Souse");
break;
}
default:
return;
}
}