Check if value in array contains a certain word

I have an array, contains two strings, please see my file attachments. How can I check if my array contains only "Region"?  Right now it does not catch it, unless I change it to  If values.Contains("Region 01 - North") . I don't want to use For Next statement.
VBdotnet2005Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Randy PooleCommented:
Look at using a List instead of array, it contains built in methods for this, including .Contains
it_saigeDeveloperCommented:
@VBdonnet2005 There are no attachments.

However, in answer to your question, since an array is an Enumerable, you need to look at Enumerable.Contains(); e.g. -
C# Example
using System;
using System.Linq;

namespace EE_Q28737059
{
	class Program
	{
		static readonly string[] items = new[] { "This is the first item.", "Region 01 - North", "This is the third item", "Region 02 - South" };

		static void Main(string[] args)
		{
			var regions = (from item in items where item.Contains("Region") select item);
			foreach (var region in regions)
				Console.WriteLine(region);

			Console.ReadLine();
		}
	}
}

Open in new window

VB.NET Example
Module Module1
	ReadOnly items As String() = New String() {"This is the first item.", "Region 01 - North", "This is the third item", "Region 02 - South"}

	Sub Main()
		Dim regions = (From item In items Where item.Contains("Region") Select item)
		For Each region In regions
			Console.WriteLine(region)
		Next
		Console.ReadLine()
	End Sub
End Module

Open in new window

Produce the following output -Capture.JPG-saige-
VBdotnet2005Author Commented:
Did you see my sample?
1052015.png
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

VBdotnet2005Author Commented:
Any thoughts?
it_saigeDeveloperCommented:
I guess I don't fully understand what you are after.  Does any of the following help with what you are trying to accomplish?
Module Module1
	Private items As String() = New String() {"This is the first item.", "Region 01 - North"}

	Sub Main()
		Console.Write("Items contains:")
		For Each item In items
			Console.Write(" [{0}]", item)
		Next
		Console.WriteLine()
		Console.WriteLine("{0} of the items contains 'Regions'.", (From item In items Where item.Contains("Region") Select item).Count())
		Console.WriteLine()
		items = (From item In items Where Not item.Contains("Region") Select item).ToArray()
		Console.WriteLine()
		Console.Write("Items now contains:")
		For Each item In items
			Console.Write(" [{0}]", item)
		Next
		Console.WriteLine()
		Console.WriteLine("{0} of the items contains 'Regions'.", (From item In items Where item.Contains("Region") Select item).Count())
		Console.ReadLine()
	End Sub
End Module

Open in new window

Which produces the following output -Capture.JPG-saige-
Athar SyedCommented:
Based on your example, you will only find "Region" in List. It will ignore other entries which may contain as part of the entry, such as "Region01...", "Delta Region", etc... So you are in the right direction, its just that your array does not have 'only' "Region" in it to validate the IF check. Add one manually and you will see it satisfies your condition and enter the IF.
it_saigeDeveloperCommented:
@athar - Not true.  Contains will match on any string that "Contains" the term (as is) to match.

Proof of concept -
Module Module1
	ReadOnly AllAreas As String() = New String() _
		   { _
			 "Region 01 - North", _
			 "Area 51 - The Black Hole", _
			 "Region01 - North 2", _
			 "Area 52 - The place that is Area 51 + 1", _
			 "Delta Region", _
			 "Checking case sensitivity with region" _
		   }

	Sub Main()
		For Each area As String In AllAreas
			If area.Contains("Region") Then
				Console.WriteLine(area)
			End If
		Next
		Console.ReadLine()
	End Sub
End Module

Open in new window

Produces the following output -Capture.JPGAs you have probably noticed, "Checking case sensitivity with region" is not output to the console.  This is because the standard implementation of String.Contains is a case-sensitive comparison.

In light of this, there is the Enumerable.Contains extension method that does have an overload which will accept an Equality Comparer.  However because a string is a specialized class, for all intents and purposes it is really just a char array, and arrays are enumerable, the extension method expects to match based on individual characters.

Fortunately, we do have a back-up method that will still look for an instance of a term, that provides a case-insensitive comparison method and it is implemented in the String class, IndexOf.  Here is how we would use, IndexOf to retrieve *all* instances of region regardless of case -
Module Module1
	ReadOnly AllAreas As String() = New String() _
	    { _
	   "Region 01 - North", _
	   "Area 51 - The Black Hole", _
	   "Region01 - North 2", _
	   "Area 52 - The place that is Area 51 + 1", _
	   "Delta Region", _
	   "Checking case sensitivity with region" _
	    }

	Sub Main()
		For Each area As String In AllAreas
			If area.IndexOf("Region", StringComparison.OrdinalIgnoreCase) >= 0 Then
				Console.WriteLine(area)
			End If
		Next
		Console.ReadLine()
	End Sub
End Module

Open in new window

Which now produces the following output -Capture.JPGOf course, if you must have a Contains method, we can implement our own Contains Extension method:
Imports System.Globalization
Imports System.Runtime.CompilerServices

Module Module1
	ReadOnly AllAreas As String() = New String() _
	    { _
	   "Region 01 - North", _
	   "Area 51 - The Black Hole", _
	   "Region01 - North 2", _
	   "Area 52 - The place that is Area 51 + 1", _
	   "Delta Region", _
	   "Checking case sensitivity with region" _
	    }

	Sub Main()
		For Each area As String In AllAreas
			If area.Contains("Region", CompareOptions.OrdinalIgnoreCase) Then
				Console.WriteLine(area)
			End If
		Next
		Console.ReadLine()
	End Sub
End Module

Module Extensions
	<Extension()> _
	Public Function Contains(ByVal source As String, ByVal sequence As String, Optional ByVal options As CompareOptions = CompareOptions.None) As Boolean
		If sequence Is Nothing Then Throw New ArgumentNullException("sequence")
		Dim sourceEnumerator = StringInfo.GetTextElementEnumerator(source)
		Dim sequenceEnumerator = StringInfo.GetTextElementEnumerator(sequence)
		While sequenceEnumerator.MoveNext()
			If sourceEnumerator.MoveNext() Then
				If Not CultureInfo.CurrentCulture.CompareInfo.Compare(sourceEnumerator.Current, sequenceEnumerator.Current, options) = 0 Then
					sequenceEnumerator.Reset()
				End If
			Else
				Return False
			End If
		End While
		Return True
	End Function
End Module

Open in new window

Which produces the following output -Capture.JPG-saige-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Athar SyedCommented:
@Saige, My response was directed to the post author and his example. Sorry for the confusion, I should have mentioned "@VBdotnet2005".

He is checking the List(Of Object) if it contains an item "Region", Where as he should be checking if the value of item in the lists contains "Region". His check will only be valid if the list contains an Item "Region".

For each obj As Object In values
  If obj.ToLower().Contains("region") Then
    ' Blah blah blah
  End If
Next

Open in new window


However, if he intentionally meant to check an item "Region", then probably, his list does not contain that item, hence the If fails.

This is what I meant to point out to him. :)
it_saigeDeveloperCommented:
My apologies...  ;)

-saige-
Athar SyedCommented:
None required! :=)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.