C#将字符串中的char与数组进行比较 [英] C# compare char in string with array

查看:118
本文介绍了C#将字符串中的char与数组进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#如何比较字符串中的字母组合和数组。



我想要这样的东西:

string value = bbadc



string [] arr = baddc,abcdb,aabbd,caadb;



搜索结果应该是arr [1];

这可能吗?如何?

解决方案

这个怎么样?



  public   class  YourClass 
{
// ...

// 将字符串'value'中的字符与字符串数组'arr' $ b的字符串中的字符进行比较$ b // 如果不匹配则返回-1,否则返回第一个匹配的arr索引
private int CompareStringChars( string value string [] arr)
{
string s = value .Alphabetize();
for int i = 0 ; i < arr.Length; i ++)
{
if (s == arr [i] .Alphabetize()) return i;
}
return -1;
}
}

// 感谢Dot Net Perls: http://www.dotnetperls.com/alphabetize-string
public static class StringExtensions
{
// 在字符串中排序(按字母顺序排列)字符
public static string 字母顺序排列( 字符串 s)
{
char [] a = s.ToCharArray();
Array.Sort(a);
return new string (一个);
}
}


.NET 3.5及更高版本提供了Enumeralbe.SequenceEqual方法,在这种情况下可以很方便:< pre lang =C#> // required
使用 System.Linq;某些NameSpace / Class中的

//

string svalue = bbadc;

string [] arr = new string [] { baddc abcdb aabbd caadb};

// note返回匹配的匹配和索引
// 如果找不到匹配的变量返回为和-1
private bool TryParseStringMatch( string sval,< span class =code-keyword> string
[] sary, out string 匹配, out int ndx)
{
sval = new string (sval.OrderBy(ch = > ch).ToArray ());

bool isMatch = false ;

match = ;
ndx = -1;

for int i = 0 ; i < sary.Length; i ++)
{
string str = sary [i];

isMatch = sval.SequenceEqual( new string (str.OrderBy(ch) = > ch).ToArray()));

if (isMatch)
{
ndx = i;
match = str;
return true ;
}
}

return isMatch;
}

// 示例测试:将此代码放入某种方法中,或者EventHandler
string amatch;
int ndex;

if (TryParseStringMatch(svalue,arr, out amatch, out ndex))
{
// 在这里设置断点:检查值
}

如果这个方法经常被调用,或者被用来比较一些字符串和一个非常大的可能匹配列表,我将使用Peter Vegter的回答中显示的技术对字符串进行排序:将字符串转换为char [],然后对该数组进行排序...而不是如此处所示,使用Linq进行排序。



我选择以模仿以TryParse开头的频繁.NET方法的方式实现此方法。


C# how do you compare a combination of letters in a string with an array.

I would like something like this:
string value = bbadc

string[] arr = baddc, abcdb, aabbd, caadb;

The result of the search should be arr[1];
Is this possible and how?

解决方案

How about this?

public class YourClass
{
    // ...

    // Compares chars in string 'value' with chars in strings of string array 'arr'
    // returns -1 if no match, otherwise returns arr index of first match
    private int CompareStringChars(string value, string[] arr)
    {
        string s = value.Alphabetize();
        for (int i = 0; i < arr.Length; i++)
        {
            if (s == arr[i].Alphabetize()) return i;
        }
        return -1;
    }
}

// Thanks to Dot Net Perls: http://www.dotnetperls.com/alphabetize-string
public static class StringExtensions
{
    // Sorts (alphabetizes) chars in a string
    public static string Alphabetize(this string s)
    {
        char[] a = s.ToCharArray();
        Array.Sort(a);
        return new string(a);
    }
}


.NET 3.5 and later provide the Enumeralbe.SequenceEqual method which can be handy in a case like this:

// required
using System.Linq;

// in some NameSpace/Class ...

string svalue = "bbadc";

string[] arr = new string[]{"baddc", "abcdb", "aabbd", "caadb"};

// note returns both match and index of the match
// if a match is not found out variables returned as "" and -1
private bool TryParseStringMatch(string sval, string[] sary, out string match, out int ndx)
{
    sval = new string(sval.OrderBy(ch => ch).ToArray());

    bool isMatch = false;

    match = "";
    ndx = -1;

    for(int i = 0; i < sary.Length; i++)
    {
        string str = sary[i];

        isMatch = sval.SequenceEqual(new string(str.OrderBy(ch => ch).ToArray()));

        if (isMatch)
        {
            ndx = i;
            match = str;
            return true;
        }
    }

    return isMatch;
}

// sample test: put this code in some method, or EventHandler
string amatch;
int ndex;

if (TryParseStringMatch(svalue, arr, out amatch, out ndex))
{
   // set break-point here: examine values
}

If this method was something being called frequently, or was used to compare some string with a very large list of possible matches, I would order the strings by using the technique shown in Peter Vegter's answer here: converting the string to a char[], then sorting that array ... rather than, as shown here, using Linq to sort.

I've chosen to implement this method in a way that imitates the frequent .NET methods that begin with 'TryParse.


这篇关于C#将字符串中的char与数组进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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