string array search

  • Thread starter Thread starter rodchar
  • Start date Start date
R

rodchar

hey all,
i have a string[] array1 populated by the .Split method. is it possible to
search for a partial string in the elements and pull that string out all in
one nice little package?

i'm trying the array1.Contains("ds=") but it doesn't work with partial
strings.

thanks,
rodchar
 
If you are using .net 3.5 then

array1.FirstOrDefault<string>(c => c.Contains("ds="))

It will return the array element or a default, my default is set to
null.

Hope this helps
jake
 
rodchar said:
hey all,
i have a string[] array1 populated by the .Split method. is it possible to
search for a partial string in the elements and pull that string out all in
one nice little package?

i'm trying the array1.Contains("ds=") but it doesn't work with partial
strings.

thanks,
rodchar

Hi rodchar,

For arrays you need to use static methods on the Array class. There is a
static method FindAll that should work.

string[] filtered = Array.FindAll(array1, delegate(string str)
{
return str.Contains("e");
});

filtered will contain all strings that have the 'e' character
 
Morten Wennevik said:
For arrays you need to use static methods on the Array class. There is a
static method FindAll that should work.

string[] filtered = Array.FindAll(array1, delegate(string str)
{
return str.Contains("e");
});

filtered will contain all strings that have the 'e' character

Unless of course you're using .NET 3.5:

var filtered = array1.Where(str => str.Contains("e"));
 
hey all,
i have a string[] array1 populated by the .Split method. is it possible to
search for a partial string in the elements and pull that string out all in
one nice little package?

i'm trying the array1.Contains("ds=") but it doesn't work with partial
strings.

thanks,
rodchar

Hi,

Array.Find( array1, delegate(string s){ return
s.Contains( yourString);} );

you also can use Exists in a same manner.


I'm sure something smaller can be done with Linq
 
rodchar said:
hey all,
i have a string[] array1 populated by the .Split method. is it possible to
search for a partial string in the elements and pull that string out all in
one nice little package?

i'm trying the array1.Contains("ds=") but it doesn't work with partial
strings.

thanks,
rodchar

There are several fancy methods to search through the items in the array
(as the replies so far demonstrates), but they all have to loop through
the items to do that. There is no way around that once you have split
the string into an array of strings.

Why not search in the entire string instead of splitting it?
 
Göran Andersson said:
rodchar said:
hey all,
i have a string[] array1 populated by the .Split method. is it
possible to search for a partial string in the elements and pull that
string out all in one nice little package?

i'm trying the array1.Contains("ds=") but it doesn't work with partial
strings.

There are several fancy methods to search through the items in the array
(as the replies so far demonstrates), but they all have to loop through
the items to do that. There is no way around that once you have split
the string into an array of strings.

Why not search in the entire string instead of splitting it?

It should be noted though that searching before splitting does
not have better big O characteristics just (significantly) faster.

Arne
 
Back
Top