Proneet
Proneet

Reputation: 139

How to find an index of a string(first three character) from an array

I want to find an index of a string only for first three char from an array

I have an array of month

string[] arrayEnglishMonth = { "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER" };

If I write

     int t_ciMonth=8;(AUGUST)
     int pos = Array.IndexOf(t_caMonth, arrayEnglishMonth[t_ciMonth - 1]);

But if I want the Index for only first 3 character i.e AUG how to find it?

Upvotes: 4

Views: 4429

Answers (6)

Leri
Leri

Reputation: 12535

You have two options I can think of:

  1. Linq only aproach that looks like this:

    var index = arrayEnglishMonth.Select((v, i) => new { v, i })
                                 .Where(c => c.v.StartsWith("AUG"))
                                 .Select(c => c.i)
                                 .First();
    

    This will first iterate over existing array, create enumerable of anonymous objects holding values and indexes, where predicate passed in Where returns true, after that select only index and take the first element from enumerable.

    Demo

  2. Find respective month using Linq and then use IndexOf method:

    var item = arrayEnglishMonth.First(c => c.StartsWith("AUG"));
    var index = Array.IndexOf(arrayEnglishMonth, item);
    

    Demo

Upvotes: 4

Tomer W
Tomer W

Reputation: 3443

Try using the following, i think it is the fastest
Array.FindIndex(strArray, s => s.StartsWith("AUG"));

Cheers.

Upvotes: 0

wdavo
wdavo

Reputation: 5110

arrayEnglishMonth.ToList().FindIndex(s => s.Substring(0,3) == "AUG");

Upvotes: 6

Alex Filipovici
Alex Filipovici

Reputation: 32561

If the t_caMonth has upper casing and it's values have only 3 letters, you may use:

 int pos = Array.IndexOf(t_caMonth, arrayEnglishMonth[t_ciMonth - 1]
     .Substring(0,3));

In order to manage casing and values with more than 3 characters, you could have:

var pos = -1;
var sel = t_caMonth
    .Select((i, index) => new { index, i = i.ToUpper() })
    .Where(i => 
        i.i.Substring(0,3) == arrayEnglishMonth[t_ciMonth - 1].Substring(0, 3))
    .Select(i => i.index);
if (sel.Count() > 0)
    pos = sel.FirstOrDefault();

You may also create a List<string> from your t_caMonth array:

var pos2 = t_caMonth
    .ToList()
    .FindIndex(i => 
        i.ToUpper().Substring(0, 3) == 
            arrayEnglishMonth[t_ciMonth - 1].Substring(0, 3));

Upvotes: 0

p.s.w.g
p.s.w.g

Reputation: 149020

You can do this with a little Linq:

string[] arrayEnglishMonth = { "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER" };
string[] t_caMonth = { ... };
string search = arrayEnglishMonth[7].Substring(0, 3); // "AUG";
int pos = t_caMonth
    .Select((s, i) => new { s, i }).Dump()
    .Where(x => x.s == search)
    .Select(x => x.i)
    .DefaultIfEmpty(-1).First();

Or more simply:

int pos = t_caMonth.TakeWhile(s => s != search).Count();

Although this last solution will return t_caMonth.Length instead of -1 if no matching element is found.

Upvotes: 2

tray2002
tray2002

Reputation: 188

This is mine way to achieve this

string[] arrayEnglishMonth = { "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER" };
var searcheditem = arrayEnglishMonth.FirstOrDefault(item => item.Substring(0, 3) == "AUG");
if(searcheditem != null)
var itemIndex = Array.IndexOf(arrayEnglishMonth, searcheditem);

but @wdavo answer is better

Upvotes: 0

Related Questions