假设我需要比较字符串x是"A","B"还是"C".
使用Python,我可以使用运算符来轻松检查.
if x in ["A","B","C"]:
do something
Run Code Online (Sandbox Code Playgroud)
使用C#,我可以做到
if (String.Compare(x, "A", StringComparison.OrdinalIgnoreCase) || ...)
do something
Run Code Online (Sandbox Code Playgroud)
它可以与Python更相似吗?
我需要添加System.Linq以便使用不区分大小写的Contain().
using System;
using System.Linq;
using System.Collections.Generic;
class Hello {
public static void Main() {
var x = "A";
var strings = new List<string> {"a", "B", "C"};
if (strings.Contains(x, StringComparer.OrdinalIgnoreCase)) {
Console.WriteLine("hello");
}
}
}
Run Code Online (Sandbox Code Playgroud)
要么
using System;
using System.Linq;
using System.Collections.Generic;
static class Hello {
public static bool In(this string source, params string[] list)
{
if (null == …Run Code Online (Sandbox Code Playgroud)