简体   繁体   English

C#中的运算符

[英]operators in C#

I have an array of words and I want to remove any word that matched the pattern and also remove each word less than 3 characters of length. 我有一个单词数组,我想删除所有与模式匹配的单词,并且还删除每个少于3个字符的单词。 Is the following code able to do what i had in mind? 以下代码是否可以完成我的想法? thanks in advance. 提前致谢。

           foreach (Match mtch in mc )

           {
               string p = @"[A-Z,a-z,0-9]" ;

               if (Regex.IsMatch(mtch.Value, p)  || mtch.Length < 3 )
                {
                   continue;
                }
              else
              {
               filter.Add(mtch.Value);
              }
           }

because I am really confused with using || 因为我真的对使用||感到困惑 operator. 运营商。

|| || (OR Operator) works in the following manner. (或运算符)以以下方式工作。 If condition 1 returns true and condition 2 returns false, the overall result is true. 如果条件1返回true,而条件2返回false,则总结果为true。

true || true = true

true || false = true

false || true = true

flase || false = false

In your case: 在您的情况下:

  if (Regex.IsMatch(mtch.Value, p)  || mtch.Length < 3 )

it is checking if the value of mtch is matching the RegEx or the lenght of mtch is less then 3 then continue the loop execution. 它正在检查mtch的值是否匹配RegEx或mtch的长度小于3,然后继续执行循环。 Now here you have two conditions, this will only go to the else part of if statement if the result of both conditions is false. 现在,这里有两个条件,如果两个条件的结果均为假,则这仅会进入if语句的else部分。 ie value of mtch is not matching the Regex p and value mtch length is greater than or equal to 3 即mtch的值与正则表达式p不匹配,并且mtch的值大于或等于3

The conditional-OR operator (||) performs a logical-OR of its bool operands. 条件或运算符(||)对它的布尔操作数执行逻辑或。 If the first operand evaluates to true, the second operand isn't evaluated. 如果第一个操作数的值为true,则不计算第二个操作数。 If the first operand evaluates to false, the second operator determines whether the OR expression as a whole evaluates to true or false. 如果第一个操作数的计算结果为false,则第二个运算符将确定OR表达式作为一个整体的计算结果为true还是false。

|| || Operator 操作者

The || || operator performs a logical-or. 运算符执行逻辑或。

It will first evaluate the left operand, if that's true then the expression will be true. 它将首先计算左操作数,如果为true,则表达式为true。 If the left operand is false, the second operand will be evaluated. 如果左操作数为false,则将评估第二个操作数。

If you run the following example as a console application you will see what happens: 如果将以下示例作为控制台应用程序运行,将看到发生的情况:

using System;

namespace OrOperand
{
    class Program
    {
        static void Main(string[] args)
        {
            DisplayResult(true, true);
            DisplayResult(true, false);
            DisplayResult(false, true);
            DisplayResult(false, false);
        }

        private static void DisplayResult(bool left, bool right)
        {
            _left = left;
            _right = right;

            Console.WriteLine("Running " + left + " || " + right);

            if (Left() || Right())
            {
                Console.WriteLine(_left + " || " + _right + " is true");
            }
            else
            {
                Console.WriteLine(_left + " || " + _right + " is false");
            }
        }

        static bool _left = true;
        static bool _right = false;

        static bool Left()
        {
            Console.WriteLine("Left called");
            return _left;
        }

        static bool Right()
        {
            Console.WriteLine("Right called");
            return _right;
        }

    }
}

This will output: 这将输出:

Running True || True
Left called
True || True is true
Running True || False
Left called
True || False is true
Running False || True
Left called
Right called
False || True is true
Running False || False
Left called
Right called
False || False is false

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

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