Mika Jones
Mika Jones

Reputation: 307

Check if a string contains a certain word only?

I am trying to find a way to check if a string contains a certain word only, or a certain word AND a date. Assume that the string can have multiple words (like a short sentence), and a date or dates (separated by hyphen). For example, if the certain word is "On" or "on", then...

string | result
"On" | true
"On 4/31" | true
"on 4/31/2018" | true
"4/13 On" | true
"4/31-5/4 on" | true
"4/31 - 5/4 on" | true
"Turn on" | false
"Turn on 5/4" | false
"On site" | false
"On 4/15 abc" | false
<Any other string that does not contain "on" at all> | false

As you can see, for the string to be true, must have "on" somewhere in the string, and it can also have a date or dates (with a hyphen) expression as well. However, once it contains another alphabetical word like "turn" or "abc" or anything else, then it needs to return false. "On" or "on" needs to appear once and that needs to be the only word in the entire string. The order does not matter, so "on" or "On" can happen at the beginning or the end of the string"

Is there any way to check this? I first thought about str.Contains("On") but this returns true for "Turn on" too, when it needs to return false. str.StartsWith("On") or str.EndsWith("On") also does not work.

By the way, "On" and "on" are both okay, so I am going to ToLower the string before the check so "On" and "on" will both result in the same result.

I hope my question makes sense, and can someone give me advice on how I could achieve this?

Edit: Here is some of the code I have written, although it is not complete as I am still struggling.

//str is the string to be examined
private bool IsValidString(string str)
{
    //Lowercase the original string 
    string lower = str.ToLower();

    //Get the number of occurrences of the substring "on"
    int occurrence = System.Text.RegularExpressions.Regex.Matches(lower, "on").Count;

    //If the occurrence count is 0, or more than 1, return false
    if (occurrence == 0 || occurrence > 1)
            return false;

    //Split the string by ' '
    string[] strArr = lower.Split(' ');

    for (int i = 0; i <= strArr.Length; i++)
    {
        //struggling in here... need to check if any other word
        //is included in the array. Still date expression is allowed.
    }
}

Upvotes: 1

Views: 1153

Answers (3)

Mong Zhu
Mong Zhu

Reputation: 23732

As I already described in my comment:

private bool IsValidString(string str)
{
    //Lowercase the original string 
    string lower = str.ToLower();
    //split by space. 
    List<string> parts = lower.Split(' ').ToList();

    //Then check if it contains "on" 
    if (parts.Contains("on"))
    {
        // get rid of the disturbing influence of "on"
        parts.Remove("on");
        //and furthermore check if the rest contains at least on further letter. This allows for numbers to appear
        if (parts.Any(x => x.ToCharArray().Any(y=> char.IsLetter(y))))
        {
            return false;
        }
        // CHECK here for the date format
        return true; // here only "on2 exists in the string as a word
    }
    else // if no "on" then it is false anyway
    {
        return false;
    }   
}

You can use Char.IsLetter to check whether the rest of the string contains any letters

Edit: This code will parse all the examples you have given. The check for the date format you need to include at the point that I indicated with a comment

Upvotes: 2

Daniel Frykman
Daniel Frykman

Reputation: 62

If I understand your question correctly you want to check if the string contains "On" or "on" but you will make it lower so it will only be "on"?

You could split the the string on spaces and then check if matches "on" I guess. Here is just a quick example. You can probably make this in a better way but it's just a PoC

string example = "On 4/15 abc";
string[] words = example.Split(' ');
bool match = false;
foreach(string word in words)
{
    if (word.Any(x => char.IsLetter(x))) {
        if (word.ToLower() == "on") 
        {
            match = true;
        } 
        else 
        { 
            match = false; break;
        }
    }
}

Upvotes: 1

Saeed Bolhasani
Saeed Bolhasani

Reputation: 580

in this case, you have to use a syntax analyzer that tell you the role of the word in the sentence. so if it is preposition we consider it false and so on. here is a library that implemented an english syntax analyzer.

Upvotes: 0

Related Questions