- Home /
String cases in C#
Please have a look at the below code
if( wordResult.Word.StringValue=="Task"){ Screens screen2 = screen.GetComponent(); screen2.Task(); } In the above code I want to perform a task if the word "Task" is detected. But I want the method to trigger even if it detects "task" or "TASK" . Now there is a way
if( wordResult.Word.StringValue=="Assignment" || wordResult.Word.StringValue=="assignment"){ Screens screen1 = screen.GetComponent(); screen1.Assignment();
But I don't want to use this as it will create different instances and is very tiring. Is there any other way ? Thank you
Answer by RudyTheDev · Jan 03, 2014 at 11:08 AM
You probably just want ToLower() (assuming StringValue is string):
if (wordResult.Word.StringValue.ToLower() == "task") ...
Can I just do something more in the if() to trigger for "tasks" (plural) also. Thank you
But I don't want to use this as it will create different instances
ToLower() creates new instances of the string where string.Equals doesn't (Just FYI)
I would do:
string word = wordResult.Word.StringValue.ToLower();
if (word == "task" || word == "tasks") ...
P.S. That's a good point by robhuhn, ToLower() does make a new string instance, which can be unwanted.
Answer by robhuhn · Jan 03, 2014 at 10:56 AM
Try following:
string.Equals(wordResult.Word.StringValue, "Task", StringComparison.OrdinalIgnoreCase)
Answer by YoungDeveloper · Jan 03, 2014 at 10:56 AM
You could compare it to ascII table. http://www.asciitable.com/
string name = "tAsK";
if(
((name[0]== 84)||(name[0]== 116)) &&
((name[1]== 65)||(name[1]== 97)) &&
((name[2]== 83)||(name[2]== 115)) &&
((name[3]== 75)||(name[3]== 107)) &&
){
}
Your answer

Follow this Question
Related Questions
Separating two strings 1 Answer
convert string to list of lists 1 Answer
Is it possible to name a list by a string variable? 2 Answers
Creating a Token At function 0 Answers
Remove the last word from a string 2 Answers