相关疑难解决方法(0)

与C#进行多个字符串比较

假设我需要比较字符串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)

c# string

23
推荐指数
3
解决办法
4万
查看次数

标签 统计

c# ×1

string ×1