简体   繁体   English

C#如何能够像TeXT一样放置“文本”,它仍然运行If

[英]C# How to be able to put “text” like TeXT and it still runs the If

I have a book for C# (CSharp) I only know a little bit but I am doing Console.Writeline stuff 我有一本关于C#的书(CSharp)我只知道一点点,但我正在做Console.Writeline的东西

I have this simple code here 我这里有这个简单的代码

Console.WriteLine("Please enter your name");

string name = Console.ReadLine();
if (name.Contains("no"))
{
    Console.WriteLine("\nFine, don't put your name in");
}
else
{
    Console.WriteLine("\nHello, " + name);
}

At the If part If you put "no" obviously it runs ("\\nFine, don't put your name in") but if you put "no" as "No" "NO" "nO" it doesn't is there a code like name.Contains where it doesn't matter how you put the text it runs it anyway 在If部分如果你把“no”显然运行(“\\ n精细,不要把你的名字放在”中),但是如果你把“no”改为“No”“NO”“nO”它就不存在像name.Contains这样的代码无论如何放置它运行它的文本并不重要

Like SmallBasic Text.ConvertToLowerCase would convert your text to lowercase and then Run the IF 像SmallBasic Text.ConvertToLowerCase会将您的文本转换为小写,然后运行IF

Thanks! 谢谢!

You should change Contains to Equals , if not a name like Noel will output "Fine don't put your name in" 您应该将Contains更改为Equals ,如果不是像Noel这样的名称将输出“Fine be not your name in”

if(name.Equals("no", StringComparison.CurrentCultureIgnoreCase))
{

}

You can convert the input to lower case characters: 您可以将输入转换为小写字符:

if (name.ToLower().Contains("no"))

But why don't you like names like "Tino" or "Nora"? 但是你为什么不喜欢“Tino”或“Nora”这样的名字呢? Better compare the whole string instead of just checking if it contains "no": 更好地比较整个字符串而不仅仅是检查它是否包含“否”:

if (name.Equals("no", StringComparison.InvariantCultureIgnoreCase))

Just use name.ToLower() method which will make the text all lower case. 只需使用name.ToLower()方法,它将使文本全部小写。 Therefore when you make your comparison to "no" or "yes" you don't have to worry about the case sensitivity of the input. 因此,当您对“否”或“是”进行比较时,您不必担心输入的区分大小写。

Use ToLower() 使用ToLower()

    string name = Console.ReadLine();
    if (name.ToLower().Contains("no"))
    {
        Console.WriteLine("\nFine, don't put your name in");
    }
    else
    {
        Console.WriteLine("\nHello, " + name);
    }

I will answer the original question: 我会回答原来的问题:

is there a code like name.Contains where it doesn't matter how you put the text it runs it anyway 是否有像name.Contains这样的代码无论如何放置它运行它的文本都无关紧要

Yes: 是:

bool contains = name.IndexOf("no", StringComparison.OrdinalIgnoreCase) >= 0;

See more information and a case-insensitive StringExtension here: Case insensitive 'Contains(string)' 在此处查看更多信息和不区分大小写的StringExtension: 不区分大小写的'Contains(string)'

However, as others have pointed out, Contains() will give you many false positives. 但是,正如其他人所指出的, Contains()会给你很多误报。 You should use .Equals() instead. 你应该使用.Equals()代替。

Use Equals method to compare your value instead of checking for Contains. 使用Equals方法比较您的值而不是检查Contains。 You have three variations to choose the equality from while ignoring cases, which are current culture case ignore, invariant culture case ignore and ordinal case ignore. 在忽略案例时,您有三种变体来选择平等,这是当前文化案例忽略,不变文化案例忽略和序数案例忽略。

Your problem does not seem to be culture specific so you can stick with StringComparison.CurrentCultureIgnoreCase . 您的问题似乎不是特定于文化的,因此您可以坚持使用StringComparison.CurrentCultureIgnoreCase In the mean time you can learn differences of using it here 与此同时,您可以在此处了解使用它的不同之

最简单的方法是做这样的事情:

if (name.ToUpper().Contains("NO"))

If you use .Contains("no") it would trigger if someone's name contains "no" 如果使用.Contains("no") ,如果某人的姓名包含“no”,则会触发

You can use this: 你可以用这个:

if(name.ToLower().Equals("no"))
{
    Console.WriteLine("\nFine, don't put your name in");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM