Home > Enterprise >  How do I find a single string from multiple arrays?
How do I find a single string from multiple arrays?

Time:12-03

I have these arrays of Samsung Galaxy A Series models

        /*            D E V I C E  M O D E L  L I S T           */

    // S A M S U N G

    //Galaxy A Series:

       //A0X
    string[] samsung_A01 =      { "SM-A015A", "SM-A015AZ", "SM-A015F", "SM-A015G", "SM-A015M", "SM-A015T1", "SM-A015U", "SM-A015U1", "SM-A015V", "SM-A015V" };
    string[] samsung_A01_Core = { "SM-A013F", "SM-A013G", "SM-A013M" };
    string[] samsung_A02 =      { "SM-A022F", "SM-A022G", "SM-A022M" };
    string[] samsung_A02s =     { "SM-A025A", "SM-A025AZ", "SM-A025F", "SM-A025G", "SM-A025M", "SM-A025U", "SM-A025U1", "SM-A025V", "SM-S124DL" };
    string[] samsung_A03_Core = { "SM-A032F", "SM-A032M" };
    string[] samsung_A03s =     { "SM-A037F", "SM-A037G", "SM-A037M" };
    
      //A1X
    string[] samsung_A10 =     { "SM-A105F", "SM-A105FN", "SM-A105G", "SM-A105M", "SM-A105N" };
    string[] samsung_A10e =    { "SM-A102U", "SM-A102U1", "SM-A102W", "SM-A102W", "SM-S102DL", "SM-A102N", };
    string[] samsung_A10s =    { "SM-A107F", "SM-A107M" };
    string[] samsung_A11 =     { "SM-A115A", "SM-A115AP", "SM-A115AZ", "SM-A115F", "SM-A115M", "SM-A115U", "SM-A115U1", "SM-A115W" };
    string[] samsung_A12 =     { "SM-A125F", "SM-A125M", "SM-A125N", "SM-A127F", "SM-A127M", "SM-A125U", "SM-A125U1", "SM-A125W", "SM-S127DL" };
    
      //A2X
    string[] samsung_A2_Core = { "SM-A260F", "SM-A260G" };
    string[] samsung_A20 =     { "SC-02M", "SCV46", "SCV46-j", "SCV46-u", "SM-A205F", "SM-A205FN", "SM-A205G", "SM-A205GN", "SM-A205W", "SM-A205YN", "SM-A205U", "SM-A205U1", "SM-S205DL" };
    string[] samsung_A20e =    { "SM-A202F" };
    string[] samsung_A20s =    { "SM-A2070", "SM-A207F", "SM-A207M" };
    string[] samsung_A21 =     { "SC-42A", "SCV49", "SM-A215U", "SM-A215U1", "SM-A215W", "SM-S215DL" };
    string[] samsung_A21s =    { "SM-A217F", "SM-A217M", "SM-A217N" };
    string[] samsung_A22 =     { "SM-A225F", "SM-A225M" };
    string[] samsung_A22_5G =  { "SC-56B", "SM-A226B", "SM-A226BR" };
    string[] samsung_A22s_5G = { "SM-A226B" };

      //A3X
    string[] samsung_A30 =     { "SCV43", "SCV43-j", "SCV43-u", "SM-A305F", "SM-A305FN", "SM-A305G", "SM-A305GN", "SM-A305GT", "SM-A305N", "SM-A305YN" };
    string[] samsung_A30s =    { "SM-A307FN", "SM-A307G", "SM-A307GN", "SM-A307GN", "SM-A307GT" };
    string[] samsung_A31 =     { "SM-A315F", "SM-A315G", "SM-A315N" };
    string[] samsung_A32 =     { "SM-A325F", "SM-A325M", "SM-A325N" };
    string[] samsung_A32_5G =  { "SCG08", "SM-A326B", "SM-A326BR", "SM-A326U", "SM-A326U1", "SM-A326W", "SM-S326DL" };

      //A4X
    string[] samsung_A40 =     { "SM-A405FM", "SM-A405FN", "SM-A405S" };
    string[] samsung_A40s =    { "SM-A3051", "SM-A3058" };
    string[] samsung_A41 =     { "SC-41A", "SCV48", "SM-A415F" };
    string[] samsung_A42_5G =  { "SM-A4260", "SM-A426B", "SM-A426N", "SM-A426U", "SM-A426U1", "SM-S426DL" };

      //A5X
    string[] samsung_A50 =    { "SM-A505F", "SM-A505FM", "SM-A505FN", "SM-A505G", "SM-A505GN", "SM-A505GT", "SM-A505N", "SM-A505U", "SM-A505U1", "SM-A505W", "SM-A505YN", "SM-S506DL" };
    string[] samsung_A50s =   { "SM-A5070", "SM-A507FN" };
    string[] samsung_A51 =    { "SM-A515F", "SM-A515U", "SM-A515U1", "SM-A515W", "SM-S515DL" };
    string[] samsung_A51_5G = { "SC-54A", "SCG07", "SM-A5160", "SM-A516B", "SM-A516N", "SM-A516U", "SM-A516U1", "SM-A516V" };
    string[] samsung_A52 =    { "SM-A525F", "SM-A525M" };
    string[] samsung_A52_5G = { "SC-53B", "SM-A5260", "SM-A526B", "SM-A526N", "SM-A526U", "SM-A526U1", "SM-A526W", "SM-A528B", "SM-A528N" };

      //A6X
    string[] samsung_A60 = { "SM-A6060", "SM-A606Y" };
    
      //A7X
    string[] samsung_A70 = { "SM-A7050", "SM-A705F", "SM-A705FN", "SM-A705GM", "SM-A705MN", "SM-A705U", "SM-A705W", "SM-A705YN" };
    string[] samsung_A70s = { "SM-A7070", "SM-A707F" };
    string[] samsung_A71 = { "SM-A715F", "SM-A715W" };
    string[] samsung_A71_5G = { "SM-A7160", "SM-A716B", "SM-A716U", "SM-A716U1", "SM-A716V" };

      //A8X
    string[] samsung_A80 = { "SM-A8050", "SM-A805F", "SM-A805N" };

      //A9X
    string[] samsung_A90_5G = { "SM-A9080", "SM-A908B", "SM-A908N" };

I programmed it to scan the phone model, but I want it to check if the model matches one of the strings in these arrays it opens a specefic panel for its model, I've tried using

                adb_modelnum = modelnum.StandardOutput.ReadToEnd();

to get the model from the phone then using

                foreach (string adb_modelnum)
            {
                if (stringToCheck.Contains(adb_modelnum))
                {

                }

but it gives me errors, any suggestions?

CodePudding user response:

you must concat all arrays like, and then use Contains using Linq

samsung_A01.Concat(samsung_A01_Core)
....
.Contains(adb_modelnum)

CodePudding user response:

You can concat all those arrays into one and then use linq, you can choose another structures to store your model(see comments) or there is another way, generics. I assume that my class called SamsungASeries will contain only string[] arrays (you can encapsulate these arrays into class like bellow)

public class SamsungASeries
{
        /*            D E V I C E  M O D E L  L I S T           */

        // S A M S U N G

        //Galaxy A Series:

        //A0X
        string[] samsung_A01 = { "SM-A015A", "SM-A015AZ", "SM-A015F", "SM-A015G", "SM-A015M", "SM-A015T1", "SM-A015U", "SM-A015U1", "SM-A015V", "SM-A015V" };
        string[] samsung_A01_Core = { "SM-A013F", "SM-A013G", "SM-A013M" };
        string[] samsung_A02 = { "SM-A022F", "SM-A022G", "SM-A022M" };
        string[] samsung_A02s = { "SM-A025A", "SM-A025AZ", "SM-A025F", "SM-A025G", "SM-A025M", "SM-A025U", "SM-A025U1", "SM-A025V", "SM-S124DL" };
        string[] samsung_A03_Core = { "SM-A032F", "SM-A032M" };
        string[] samsung_A03s = { "SM-A037F", "SM-A037G", "SM-A037M" };

        //A1X
        string[] samsung_A10 = { "SM-A105F", "SM-A105FN", "SM-A105G", "SM-A105M", "SM-A105N" };
        string[] samsung_A10e = { "SM-A102U", "SM-A102U1", "SM-A102W", "SM-A102W", "SM-S102DL", "SM-A102N", };
        string[] samsung_A10s = { "SM-A107F", "SM-A107M" };
        string[] samsung_A11 = { "SM-A115A", "SM-A115AP", "SM-A115AZ", "SM-A115F", "SM-A115M", "SM-A115U", "SM-A115U1", "SM-A115W" };
        string[] samsung_A12 = { "SM-A125F", "SM-A125M", "SM-A125N", "SM-A127F", "SM-A127M", "SM-A125U", "SM-A125U1", "SM-A125W", "SM-S127DL" };

        //A2X
        string[] samsung_A2_Core = { "SM-A260F", "SM-A260G" };
        string[] samsung_A20 = { "SC-02M", "SCV46", "SCV46-j", "SCV46-u", "SM-A205F", "SM-A205FN", "SM-A205G", "SM-A205GN", "SM-A205W", "SM-A205YN", "SM-A205U", "SM-A205U1", "SM-S205DL" };
        string[] samsung_A20e = { "SM-A202F" };
        string[] samsung_A20s = { "SM-A2070", "SM-A207F", "SM-A207M" };
        string[] samsung_A21 = { "SC-42A", "SCV49", "SM-A215U", "SM-A215U1", "SM-A215W", "SM-S215DL" };
        string[] samsung_A21s = { "SM-A217F", "SM-A217M", "SM-A217N" };
        string[] samsung_A22 = { "SM-A225F", "SM-A225M" };
        string[] samsung_A22_5G = { "SC-56B", "SM-A226B", "SM-A226BR" };
        string[] samsung_A22s_5G = { "SM-A226B" };

        //A3X
        string[] samsung_A30 = { "SCV43", "SCV43-j", "SCV43-u", "SM-A305F", "SM-A305FN", "SM-A305G", "SM-A305GN", "SM-A305GT", "SM-A305N", "SM-A305YN" };
        string[] samsung_A30s = { "SM-A307FN", "SM-A307G", "SM-A307GN", "SM-A307GN", "SM-A307GT" };
        string[] samsung_A31 = { "SM-A315F", "SM-A315G", "SM-A315N" };
        string[] samsung_A32 = { "SM-A325F", "SM-A325M", "SM-A325N" };
        string[] samsung_A32_5G = { "SCG08", "SM-A326B", "SM-A326BR", "SM-A326U", "SM-A326U1", "SM-A326W", "SM-S326DL" };

        //A4X
        string[] samsung_A40 = { "SM-A405FM", "SM-A405FN", "SM-A405S" };
        string[] samsung_A40s = { "SM-A3051", "SM-A3058" };
        string[] samsung_A41 = { "SC-41A", "SCV48", "SM-A415F" };
        string[] samsung_A42_5G = { "SM-A4260", "SM-A426B", "SM-A426N", "SM-A426U", "SM-A426U1", "SM-S426DL" };

        //A5X
        string[] samsung_A50 = { "SM-A505F", "SM-A505FM", "SM-A505FN", "SM-A505G", "SM-A505GN", "SM-A505GT", "SM-A505N", "SM-A505U", "SM-A505U1", "SM-A505W", "SM-A505YN", "SM-S506DL" };
        string[] samsung_A50s = { "SM-A5070", "SM-A507FN" };
        string[] samsung_A51 = { "SM-A515F", "SM-A515U", "SM-A515U1", "SM-A515W", "SM-S515DL" };
        string[] samsung_A51_5G = { "SC-54A", "SCG07", "SM-A5160", "SM-A516B", "SM-A516N", "SM-A516U", "SM-A516U1", "SM-A516V" };
        string[] samsung_A52 = { "SM-A525F", "SM-A525M" };
        string[] samsung_A52_5G = { "SC-53B", "SM-A5260", "SM-A526B", "SM-A526N", "SM-A526U", "SM-A526U1", "SM-A526W", "SM-A528B", "SM-A528N" };

        //A6X
        string[] samsung_A60 = { "SM-A6060", "SM-A606Y" };

        //A7X
        string[] samsung_A70 = { "SM-A7050", "SM-A705F", "SM-A705FN", "SM-A705GM", "SM-A705MN", "SM-A705U", "SM-A705W", "SM-A705YN" };
        string[] samsung_A70s = { "SM-A7070", "SM-A707F" };
        string[] samsung_A71 = { "SM-A715F", "SM-A715W" };
        string[] samsung_A71_5G = { "SM-A7160", "SM-A716B", "SM-A716U", "SM-A716U1", "SM-A716V" };

        //A8X
        string[] samsung_A80 = { "SM-A8050", "SM-A805F", "SM-A805N" };

        //A9X
        string[] samsung_A90_5G = { "SM-A9080", "SM-A908B", "SM-A908N" };
    }

Then you can get all private fields, cast them as a string array and try to find your model. Here is a snippet from the console app.

static void Main(string[] args)
{
        var expectedModel = "SM-A015AZ";
        var series = new SamsungASeries();
        var allFields = series.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance);

        string model = null;
        foreach (var item in allFields)
        {
            var modelArray = (string[])item.GetValue(series);
            model = modelArray.FirstOrDefault(x => x.Equals(expectedModel));
            if(model != null)
            {
                break;
            }
        }

        if(model != null)
        {
            Console.WriteLine($"Found: {model}");
        }
        else
        {
            Console.WriteLine($"Not Found");
        }

        Console.ReadKey(true);
 }

Not the best way but I think the fastest one (if you don't want to refactor your code).

  • Related