C# StringReader Class
Asked Answered
C

4

6

I have this problem, I'm using StringReader to find specific words from a textbox, so far is working great, however I need to find a way how to check specific words in every line against a string array.

The following code works:

string txt = txtInput.Text;
string user1 = "adam";
int users = 0;
int systems = 0;

using (StringReader reader = new StringReader(txt))
{
    while ((txt = reader.ReadLine()) != null)
    {
        if (txt.Contains(user1))
        {
            users++;
        }
    }
}

Now, I have created a String Array to store more than one string, but the method Contains seems to only accept a string.

string[] systemsarray = new string[] { "as400", "x500", "mainframe" };

if(txt.Contains(systemsarray))
{
    systems++;
}
// error message: cannot convert from string[] to string

Does anyone have an idea how to do this, or a way to improve it?

Thanks in advance.

Coen answered 5/8, 2013 at 19:3 Comment(0)
W
9

If you are looking for the existence of any of those words in the line, try:

if(systemsarray.Any(word => txt.Contains(word)))
{
    users++;
}
Wizardly answered 5/8, 2013 at 19:6 Comment(4)
Or simply if (systemsarray.Any(txt.Contains))Joachima
Hard to tell for certain what the OP wants, but if they need a count, replace Any with Count and store that in systems.Contredanse
@Moo-Juice The answer HocsanMoya selected is effectively the same as this one, only unrolled in a loop instead of as a one liner using LINQ.Wizardly
Hi, I just did, I'm sorry about that.Coen
E
5

Why not write yourself an extension method to do this?

public static class StringExtensionMethods
{
    public static bool ContainsAny(this string self, params string[] toFind)
    {
        bool found = false;
        foreach(var criteria in toFind)
            {
                if (self.Contains(criteria))
                {
                    found = true;
                    break;
                }
            };

        return found;
    }   // eo ContainsAny    
}

Usage:

string[] systemsarray = new string[] { "as400", "x500", "mainframe" };

if(txt.ContainsAny(systemsarray))
{
    systems++;
}
// error message: cannot convert from string[] to string
Edmanda answered 5/8, 2013 at 19:7 Comment(0)
G
5

you need to extract each item from your array:

foreach (string item in systemsarray)
{
 if(txt.Contains(item))
 {
    systems++;
 }
}
Gareth answered 5/8, 2013 at 19:8 Comment(1)
@LuameLudik, sorry to bother again, I was wondering do you know how to get rid of the duplicates? I have tried several things and still not working, I created a new String Array and tried to remove the duplicates... String[] systems2 = new string[] {item}; IEnumerable<String> distinctsystem = systems2.Distinct(); foreach (String theString in distinctsystem) { systems++; }Coen
I
2

If you want a case-insensitive search (as400 will match AS400), you can do this

if (systemsarray.Any(x => x.Equals(txt, StringComparison.OrdinalIgnoreCase)))
{
    //systemsarray contains txt or TXT or TxT etc...
}

You can remove StringComparison.OrdinalIgnoreCase if you want to take the case into account (or choose a different enum value).

Internalcombustion answered 5/8, 2013 at 19:39 Comment(3)
Hi, thanks for your help, something I did was to convert all the text to lower case; like this; string txt = txtInput.Text.ToLower(); but I will take this in consideration, thanks again!Coen
@HocsanMoya - You're welcome. As as side note, converting and checking the lowercase is not recommended. You should usually use the comparison check above or convert it to uppercase instead more info.Internalcombustion
Hi! Thanks, in that case, I will follow your advice.Coen

© 2022 - 2024 — McMap. All rights reserved.