string array search

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
 
J

jake

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
 
M

Morten Wennevik [C# MVP]

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
 
J

Jon Skeet [C# MVP]

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"));
 
I

Ignacio Machin ( .NET/ C# MVP )

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
 
G

Göran Andersson

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?
 
A

Arne Vajhøj

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top