Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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?
Posted
Comments
Member 12160712 7-Dec-15 17:53pm    
I am not sure I understand your question, have you misspelled any of the values of the array or the string value, there is no match in the array with the value!
Philippe Mori 7-Dec-15 18:06pm    
Use tag code and fix your code to show ' and ".
Patrice T 7-Dec-15 18:27pm    
You need to learn C# syntax first.
PIEBALDconsult 7-Dec-15 18:33pm    
That depends on the lengths of the strings and how many times you need to search -- if the strings are like what you present and you search only once, then just iterate the array.

How about this?

C#
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);
    }
}
 
Share this answer
 
v3
Comments
BillWoodruff 8-Dec-15 1:14am    
+5 good answer !
[no name] 8-Dec-15 4:37am    
Thanks!
.NET 3.5 and later provide the Enumeralbe.SequenceEqual method which can be handy in a case like this:
C#
// 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.
 
Share this answer
 
v2
Comments
[no name] 8-Dec-15 4:37am    
+5 doing it the Linq way!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900