C# 不区分大小写的字符串

发布于 2024-07-22 05:37:27 字数 661 浏览 6 评论 0原文

考虑下面的类
- 我可以做些什么来实现不区分大小写的字符串吗?

public class Attibute
{
    // The Name should be case-insensitive
    public string Name
    {
        get;
        set;
    }

    public Attibute()
    {
    }
}

public class ClassWithAttributes
{
    private List<Attributes> _attributes;

    public ClassWithAttributes(){}

    public AddAttribute(Attribute attribute)
    {
        // Whats the best way to implement the check?
        _attributes.add(attribute);
    }
}

HTML 结构4 文件

我已经编辑了课程,使其更加客观和具体

Considering the class below
- can I do anything to implement a case-insensitive string?

public class Attibute
{
    // The Name should be case-insensitive
    public string Name
    {
        get;
        set;
    }

    public Attibute()
    {
    }
}

public class ClassWithAttributes
{
    private List<Attributes> _attributes;

    public ClassWithAttributes(){}

    public AddAttribute(Attribute attribute)
    {
        // Whats the best way to implement the check?
        _attributes.add(attribute);
    }
}

Structure of an HTML 4 Document

I have edited the class to be a bit more objective and specific

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

作妖 2024-07-29 05:37:27

为了回答重组问题,您可以这样做:

public class Attribute { public string Name { get; set; } }

public class AttributeCollection : KeyedCollection<string, Attribute> {
    public AttributeCollection() : base(StringComparer.OrdinalIgnoreCase) { }
    protected override string GetKeyForItem(Attribute item) { return item.Name; }
}

public class ClassWithAttributes {
    private AttributeCollection _attributes;

    public void AddAttribute(Attribute attribute) {
        _attributes.Add(attribute);    
        //KeyedCollection will throw an exception
        //if there is already an attribute with 
        //the same (case insensitive) name.
    }
}

如果您使用它,您应该将 Attribute.Name 设置为只读,或者在更改时调用 ChangeKeyForItem。

In answer to the restructured question, you could do it like this:

public class Attribute { public string Name { get; set; } }

public class AttributeCollection : KeyedCollection<string, Attribute> {
    public AttributeCollection() : base(StringComparer.OrdinalIgnoreCase) { }
    protected override string GetKeyForItem(Attribute item) { return item.Name; }
}

public class ClassWithAttributes {
    private AttributeCollection _attributes;

    public void AddAttribute(Attribute attribute) {
        _attributes.Add(attribute);    
        //KeyedCollection will throw an exception
        //if there is already an attribute with 
        //the same (case insensitive) name.
    }
}

If you use this, you should either make Attribute.Name read-only or call ChangeKeyForItem whenever it's changed.

甜心小果奶 2024-07-29 05:37:27

您不能具有不区分大小写的属性 - 只能具有不区分大小写的操作,例如比较。 如果有人访问 XHtmlOneDTDElementAttibute.Name,他们将返回一个字符串,无论其创建时的大小写如何。

每当您使用 .Name 时,您都可以以忽略字符串大小写的方式实现该方法。

You can't have case-insensitive properties—you can only have case-insensitive operations, like a comparison. If someone accesses XHtmlOneDTDElementAttibute.Name, they will get back a string with whatever case it was created with.

Whenever you use .Name, you can implement that method in a way that ignores the case of the string.

橪书 2024-07-29 05:37:27

这取决于你想用字符串做什么。

如果您想要比较字符串而不考虑大小写,请使用 StringComparison.OrdinalIgnoreCase 调用 String.Equals
如果您想将它们放入字典中,请将字典的比较器设置为StringComparer.OrdinalIgnoreCase

因此,您可以创建一个函数,如下所示:

public class XHtmlOneDTDElementAttibute : ElementRegion {
    public bool IsTag(string tag) {
        return Name.Equals(tag, StringComparison.OrdinalIgnoreCase);
    }

    // The Name should be case-insensitive
    public string Name { get; set; }

    // The Value should be case-sensitive
    public string Value { get; set; }
}

如果您想要更具体的解决方案,请告诉我您正在使用 Name 属性做什么

It depends what you're trying to do with the strings.

If you want to compare strings regardless of case, call String.Equals with StringComparison.OrdinalIgnoreCase.
If you want to put them in a dictionary, make the dictionary's comparer StringComparer.OrdinalIgnoreCase.

Therefore, you could make a function as follows:

public class XHtmlOneDTDElementAttibute : ElementRegion {
    public bool IsTag(string tag) {
        return Name.Equals(tag, StringComparison.OrdinalIgnoreCase);
    }

    // The Name should be case-insensitive
    public string Name { get; set; }

    // The Value should be case-sensitive
    public string Value { get; set; }
}

If you want a more specific solution, please tell me what you're doing with the Name property

一口甜 2024-07-29 05:37:27

好吧,在浏览了规范之后,我对此的看法是,您无需执行任何操作即可使字符串属性不区分大小写。 无论如何,这个概念并没有真正意义:字符串不区分大小写或不区分大小写; 对它们的操作(如搜索和排序)是。

(我知道 W3C 的 HTML 建议本质上就是这么说的。它的措辞很糟糕。)

Well, my take on this, after glancing at the spec, is that there's nothing you need to do to make the string properties case-insensitive. The concept doesn't really make sense, anyway: strings aren't case-sensitive or -insensitive; operations on them (like search and sort) are.

(I know the W3C's HTML recommendations say essentially that. It's badly-phrased.)

白日梦 2024-07-29 05:37:27

或者,您可能希望使属性始终大写,如下所示。

public class XHtmlOneDTDElementAttibute : ElementRegion {
    string name;

    // The Name should be case-insensitive
    public string Name {
        get { return name; }
        set { name = value.ToUpperInvariant(); }
    }

    // The Value should be case-sensitive
    public string Value { get; set; }
}

Alternatively, you might want to make the property always uppercase, like this.

public class XHtmlOneDTDElementAttibute : ElementRegion {
    string name;

    // The Name should be case-insensitive
    public string Name {
        get { return name; }
        set { name = value.ToUpperInvariant(); }
    }

    // The Value should be case-sensitive
    public string Value { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文