0

This is my first question here!

I'm working on a C# coding assignment for college where the player inputs different actions and results of their actions are displayed in the console. Until now, I have simply been saying

(if firstInput == "Action" || firstInput == "action")

I have heard that I could simplify this using string.ToLower(), but I cannot seem to figure out how to.

Any help on this would be appreciated, and my apologies if it's pretty obvious, I'm a C# noob :p

Thanks!

3
  • did you try if (firstInput.ToLower() == "action") ? I mean it appears a super simple and duplicate question from what you post, and code examples are always good Commented Dec 15, 2018 at 17:51
  • You can use learn.microsoft.com/en-us/dotnet/api/… with StringComparison.InvariantCultureIgnoreCase. Commented Dec 15, 2018 at 17:52
  • 1
    Just don't get bitten by the Turkey test. Commented Dec 15, 2018 at 17:57

3 Answers 3

1

If it really matters if the user wrote the first letter in capital letter or not, you might need to compare the strings specifying not to ignore cases.

So, since "Action" != "action", give a try to String.Equals

bool isEqual = String.Equals(x, y, StringComparison.Ordinal);

So then, I would perform a .ToLower() from the user's input and then compare it with the original input.

Sign up to request clarification or add additional context in comments.

Comments

0

The idea is to convert the input to all lower case, so that you can always compare against a lower case string constant no matter what the user entered

if (firstInput.ToLower() == "action") {
    ...
}

Examples:

"ACTION".ToLower()  ===>  "action"
"Action".ToLower()  ===>  "action"
"action".ToLower()  ===>  "action"

Comments

0

string.ToLower() returns a new string containing all lowercase characters. So your code should read like this:

if (firstInput.ToLower() == "action"){
{

The .ToLower() transforms firstInput into an all-lowercase string. When you compare that with an all lowercase string literal for "action", the compare will succeed if firstInput contains "action" in any upper or lower case form (Action, action, aCtIoN, etc).

It's worth noting that the Microsoft .Net documentation tells you how to use string.ToLower(). As part of learning how to program in C#, you should get used to checking the Microsoft .Net documentation to learn how to use functionality provided by the framework. The string.ToLower() article provides a complete code sample:

using System;

public class ToLowerTest {
    public static void Main() {

        string [] info = {"Name", "Title", "Age", "Location", "Gender"};

        Console.WriteLine("The initial values in the array are:");
        foreach (string s in info)
            Console.WriteLine(s);

        Console.WriteLine("{0}The lowercase of these values is:", Environment.NewLine);        

        foreach (string s in info)
            Console.WriteLine(s.ToLower());

    }
}
// The example displays the following output:
//       The initial values in the array are:
//       Name
//       Title
//       Age
//       Location
//       Gender
//       
//       The lowercase of these values is:
//       name
//       title
//       age
//       location
//       gender

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.