VBdotnet2005
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.
Look at using a List instead of array, it contains built in methods for this, including .Contains
@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
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();
}
}
}
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
Produce the following output --saige-
ASKER
Did you see my sample?
1052015.png
1052015.png
ASKER
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
Which produces the following output --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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
My apologies... ;)
-saige-
-saige-
None required! :=)