0

I must doing something wrong... But I can't figure it out!

I have an array with string in it. I'm trying to fins if the Array contains some words like Sales for example.

drillDownUniqueNameArray[0] = "[Sales Territory].[Sales Territories].[Sales Territory Group].&[North America]";//Inside the string array there is this string in index 0

drillDownUniqueNameArray.Contains("S")//Output false!

Array.IndexOf(drillDownUniqueNameArray,"S")//Output -1!  <--Fixed My answer

drillDownUniqueNameArray.Contains("[Sales Territory].[Sales Territories].[Sales Territory Group].&[North America]") //Output true!

I thouhgt Contains should find even part of the string..

How can I find if this array have "S" or "Sales" for example?

6 Answers 6

5

You are asking if the array contains a string that exactly matches "S".

What you want is to ask if any of the strings in the array contains the character "S", something like:

drillDownUniqueNameArray.Any(v => v.Contains("S"))
Sign up to request clarification or add additional context in comments.

1 Comment

I fixed my answer... I'm getting -1
2

You're checking if the array contains an element that's exactly "S" but I think you are trying to check whether the array contains an alement that contains an "S".

You could achieve this by the following statement:

drillDownUniqueNameArray.Any( str => str.Contains ("S") )

3 Comments

I fixed my answer... I'm getting -1
+1 but explain reason too, if he was doing that way I suppose he didn't understand it.
@SimonC does Stephan was the first with this answer?
1

You can try this. drillDownUniqueNameArray[0].Contains("s");

Comments

0

You can use LINQ:

var allWithSales = drillDownUniqueNameArray
    .Where(str => str.Contains("Sales"));

ignoring the case:

var allWithSalesIgnoreCase = drillDownUniqueNameArray
     .Where(str => str.IndexOf("sales", StringComparison.OrdinalIgnoreCase) >= 0);

If you want to find all that contain a word "Sales"(String.Split() = white-space delimiter)):

var allWordsWithSales = drillDownUniqueNameArray
    .Where(str => str.Split().Contains("Sales", StringComparer.OrdinalIgnoreCase));

Now you can enumerate the query with foreach or use ToArray() or ToList to create a collection:

foreach(string str in allWithSales)
    Console.WriteLine(str);

Comments

0

You are finding it in the array, but you should find the word in the string.

Use following if you want to check:

drillDownUniqueNameArray.Any(x=>x.Contains("Sales"));

Use following if want to get the strings which contains "Sales"

drillDownUniqueNameArray.Where(x=>x.Contains("Sales"));

Comments

0

When you do it like this:

drillDownUniqueNameArray.Contains("S")

it's not gonna check the values, you must do it like this:

drillDownUniqueNameArray[0].Contains("S") or drillDownUniqueNameArray.First().Contains("S")

like this way it checks the values inside the array not the arrays itself

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.