This question already has answers here:
Is there a C# case insensitive equals operator?
(14 answers)
Closed 5 years ago.
How to compare a 2 string in different case
like
String a="Pawan";
String b="PAWAN";
how to compare using inbuilt method
I am try to compare culture method but can not be compere so please provide a solution
return result equal or not equal
Transform both to lowercase strings and compare after that
a.ToLower() == b.ToLower()
Every answer so far was checking for equality, here is mine actually comparing:
switch(string.Compare(a, b, StringComparison.CurrentCultureIgnoreCase))
{
case 1: Console.WriteLine("a is greater"); break;
case 0: Console.WriteLine("a and b are equal"); break;
case -1: Console.WriteLine("b is greater"); break;
}
You can test it here https://dotnetfiddle.net/h75xZ8
If you arent sure about the comparison, you can look it up here:
https://msdn.microsoft.com/en-us/library/system.stringcomparison(v=vs.110).aspx
Related
This question already has answers here:
Variable declaration in a C# switch statement [duplicate]
(7 answers)
Case Statement Block Level Declaration Space in C#
(6 answers)
Closed 3 years ago.
I was surprised to discover that locals in switch statements are scoped to the switch statement and not to the case. Why is that? And is it good practice to add curly braces around each case as a workaround.
This won't compile:
switch (n)
{
case 1:
var i = 1;
Console.WriteLine(i);
break;
default:
var i = 2;
Console.WriteLine(i);
break;
}
This does compile:
switch (n)
{
case 1:
{
var i = 1;
Console.WriteLine(i);
break;
}
default:
{
var i = 2;
Console.WriteLine(i);
break;
}
}
This is a design choice specifically made by C#.
A variable only exists inside the innermost braces in which the variable is first declared. This makes it easy to check the variables scope just by looking, and I might guess it also makes parsing easier
This question already has answers here:
Evaluating string "3*(4+2)" yield int 18 [duplicate]
(13 answers)
Closed 4 years ago.
I'm starting out with coding and trying to make a very basic calculator. I want to run it in a cmd by calling it up with two numbers. So, in the correct directory, I would type "BasicCal 2 + 5" to run it.
This is the code in C#
using System;
public class BasicCal
{
public static void Main(string [] args)
{
Console.Write(args[0] + args[1] + args[2]);
Console.ReadKey();
}
}
Cmd then just prints "2+5" so I guess C# doesn't see an operator as an argument.
So I just need to know how to make C# recognize an operator when it's given as a parameter. Thanks in advance.
There are couple of problems here.
A) There is no sort of built-in way that allows C# compiler to understand what you're doing here so you'd have to implement the logics of parsing string argument with the sign:
switch(args[1])
{
case "+":
{
...
break;
}
case "-":
{
...
break;
}
... etc
}
B) Args are just strings so if we just do something like args[0] + args[1] the C# compiler doesn't know that its working with numbers and runs an overloaded + operator for string which does string concatenation.
This question already has answers here:
Multi-variable switch statement in C#
(13 answers)
Closed 8 years ago.
Is it possible to return result from multiple switch statement?
For example i would like to use employee.DepartmentID and employee.StatusID for my case. But how do i include employee.StatusID in this statement? Using and/or operators?
switch (employee.DepartmentID)
{
case 1:
EMAIL = "abc#gmail.com";
break;
case 2:
EMAIL = "abcd#gmail.com";
break;
}
What you really need is this.
Use the Switch to determine which department is involved
Switch (DepartmentID)
{
case 1:
Email = classHR.GetEmailAddress(Status);
break;
case 2:
Email = classMarketing.GetEmailAddress(Status);
break;
}
Use Static Classes for the different departments (using an interface preferably).
This will give you a better run down than what you are thinking of here.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I don't know how IndexOf() method of String object works and so I would like to know which outperforms the other with the 2 following implementations:
At first, I want to introduce a little about the problem, simply, the function/method implemented here has a character as the only parameter, it should give out/return another character corresponding to the one passed in. (the rule of matching between source char collection and destination char collection is given below):
a <=> 9
b <=> 8
c <=> 7
d <=> 6
e <=> 5
f <=> 4
g <=> 3
h <=> 2
i <=> 1
j <=> 0
Please note that, the above rule is just made for easy to follow, it's not a fixed rule, it can be any so don't base on that rule to implement those methods another way.
Now is the 2 methods I would like to compare:
1. The first one is very short and based on IndexOf()
string source = "abcdefghij";
string destination = "9876543210";
public char SourceToDest(char c){
return destination[source.IndexOf(c)];//Suppose the c is always in source.
}
2. The second one is longer and uses switch case:
public char SourceToDest(char c){
switch(c){
case 'a': return '9';
case 'b': return '8';
case 'c': return '7';
case 'd': return '6';
case 'e': return '5';
case 'f': return '4';
case 'g': return '3';
case 'h': return '2';
case 'i': return '1';
case 'j': return '0';
}
}
As I mentioned before, the rule is made for easy to follow, if not noticing this, you may have another method like this:
public char SourceToDest(char c){
return (char)(154 - (int)c); //154 = 106 + 48
}
If you have another method which outperforms both the 2 methods I presented, please share with me.
You can make the other method easier to follow, and still be fast:
public char SourceToDest(char c)
{
return (char)((int)'j' - (int)c + (int)'0');
}
Another option is:
const string destination = "9876543210";
public char SourceToDest(char c)
{
return destination[(int)c - (int)'a'];
}
Which will be faster than your other two methods.
You can use SortedDictionary<char, char> in your case. Search in SortedDictionary is O(log n). Search in string with IndexOf I guess should be O(n), I don't think that it has some special optimizations (at least MSDN does not tell you that). So your example will be
SortedDictionary<char, char> encoding = new SortedDictionary<char, char>()
{
{ 'a', '9' }, { 'b', '8' } /* ... */ , { 'j', '0' }
}
public char SourceToDest(char c){
return encoding[c];
}
In general for large(r) string lengths N, the first will be O(N) by virtue of being a linear search, while the second will be O(1) as a indexed access.
For small(er) string lengths N the asymptotic performance is swamped by the constant factor, and you would have to measure hundreds of millions of accesses to get e meaningful comparison. But would you even care in these cases? Surely there are dozens of more productive performance cases to investigate in the application.
This question already has answers here:
How to make C# Switch Statement use IgnoreCase
(12 answers)
Closed 9 years ago.
C#'s switch() statement is case-sensitive. Is there a way to toggle it so it becomes case-insensitive?
==============================
Thanks,
But , I don't like these solutions;
Because case conditions will be a variable , and I don't know if they ALL are UPPER or lower.
Yes - use ToLower() or ToLowerInvariant() on its operands. For example:
switch(month.ToLower()) {
case "jan":
case "january": // These all have to be in lowercase
// Do something
break;
}
You can do something like this
switch(yourStringVariable.ToUpper()){
case "YOUR_CASE_COND_1":
// Do your Case1
break;
case "YOUR_CASE_COND_2":
// Do your Case 2
break;
default:
}
Convert your switch string to lower or upper case beforehand
switch("KEK".ToLower())
{
case "kek":
CW("hit!");
break;
}