Reputation: 582
I have an array of strings and want to output those that are of a certain length from the array.
string[]myArray = {"stringone", "stringtwo", "stringthree"};
I have tried doing
foreach(thing in myArray){
if(thing.length<10){
do stuff
}
@output
But doesnt work. Where am i going wrong? I'm using C# in asp.net.
Many thanks.
Upvotes: 1
Views: 142
Reputation: 149646
Assuming your problem was length
instead of Length
, you can filter out only the values you need using a Where
clause:
string[] myArray = { "stringone", "stringtwo", "stringthree" };
foreach (string thing in myArray.Where(thing => thing.Length < 10))
{
// Here you'll only iterate values
// whos length is less than 10
}
Upvotes: 0
Reputation: 1801
you need to specify that thing
is a string
or var
.
Also, you need to capitalize Length.
public void McTester()
{
string[] myArray = { "stringone", "stringtwo", "stringthree" };
foreach (string thing in myArray)
{
if (thing.Length < 10)
{
//do stuff
}
}
}
Upvotes: 2
Reputation: 3680
string[] myArray = { "stringone", "stringtwo", "stringthree" };
var lessThan10Length = myArray.Where(x=> x.Length < 10).ToList();
Upvotes: 0