如何使用当前的文化方法 [英] How to use current culture method

查看:91
本文介绍了如何使用当前的文化方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在程序中运行以下代码并且它没有按照我想要的方式执行比较,因为我正在Kannada(kn-IN)中执行字符串比较。所以我想使用currentculture方法执行字符串比较,以检查它是否适合我。我尝试阅读它,但我无法理解如何在此代码中实现它。请提及我必须添加的参考文献。



I'm running the following code within a program and it is not performing comparison the way I want it to, as I'm performing string comparison in Kannada(kn-IN). So I want to perform string comparison using currentculture method to check if it does the job for me. I tried reading it up but I'm not able to understand how to implement it in this code. please mention the references I have to add with your answer.

private void selectWords(string fullText, string searchText)
        {
            string[] words = fullText.Split(' ');
            ArrayList str = new ArrayList();
            foreach (var word in words)
            {
                if (word.Contains(searchText))
                {
                    richTextBox1.Find(word, 0, richTextBox1.TextLength, RichTextBoxFinds.None);
                    richTextBox1.SelectionBackColor = Color.Green;
                    str.Add(richTextBox1.SelectedText);
                }
            }

推荐答案

包含方法使用序数比较:

The Contains method uses an ordinal comparison:
public bool Contains(string value)
{
    return this.IndexOf(value, StringComparison.Ordinal) >= 0;
}



不幸的是,包含方法没有超载接受 StringComparison 选项。



幸运的是,编写自己的代码很简单:


Unfortunately, there isn't an overload of the Contains method which accepts a StringComparison option.

Fortunately, it's easy to write your own:

public static class StringExtensions
{
    public static bool Contains(this string source, string value, StringComparison comparisonType)
    {
        if (source == null) throw new ArgumentNullException("source");
        return source.IndexOf(value, comparisonType) >= 0;
    }
}





然后你需要调用扩展方法传递一个合适的 StringComparison 选项:



Then you just need to call the extension method passing in a suitable StringComparison option:

private void selectWords(string fullText, string searchText)
{
    string[] words = fullText.Split(' ');
    ArrayList str = new ArrayList();
    foreach (var word in words)
    {
        if (word.Contains(searchText, StringComparison.CurrentCulture))
        // Or: StringExtensions.Contains(word, searchText, StringComparison.CurrentCulture)
        {
            richTextBox1.Find(word, 0, richTextBox1.TextLength, RichTextBoxFinds.None);
            richTextBox1.SelectionBackColor = Color.Green;
            str.Add(richTextBox1.SelectedText);
        }
    }
}


这篇关于如何使用当前的文化方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆