Link to home
Start Free TrialLog in
Avatar of VBdotnet2005
VBdotnet2005Flag for United States of America

asked on

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.
Avatar of Randy Poole
Randy Poole
Flag of United States of America image

Look at using a List instead of array, it contains built in methods for this, including .Contains
Avatar of it_saige
@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 -User generated image-saige-
Avatar of VBdotnet2005

ASKER

Did you see my sample?
1052015.png
Any thoughts?
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 -User generated image-saige-
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.
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
My apologies...  ;)

-saige-
None required! :=)