none
Check whether text has particular words RRS feed

  • Question

  • Hello,

    I have string variable.

    String s1 = "It is a nice day today";

    string searchString = "is;day";

    -- NOTE

    Variable search string has two words delimited by semicolon (is and day)

    I want to write an expression to check whether string s1 has either of these two words (is or day) or both the words (is and day)

    I tried with s1.contains, it did not work.

    Any clue?

    Thanks

    SaiRam

    Friday, August 22, 2014 10:18 PM

Answers

  • if your search string type may change like "s1;s2;s3;s4;s5;s6;s7";

    then the Code should be like this :

    string s1 ="it is a nice day today";
    string searchString = "s1;s2;s3;s4;s5;s6;s7";
    string [] ser = searchString.split(';');
    
    // now ser can be like this: ser[0] = "blah1",
    // ser[1]="blah2" or like this : ser[0]= "blah1",
    //  ser[1] = "blah2" ser[3] = "blah3" and so on . . .
    
    int conain = 0;
    
    foreach (strign item in ser)
    {
    if (s1.contains(item))
    {
    contain ++ ;
    }
    }
    
    if(contain==ser.Getlength(0))
    {
    // it means s1 contains all of searched strings
    }
    
    
    if(contain==0)
    {
    // it means s1 contains non of searched strings
    }
    
    
    if(contain<ser.Getlength(0) && contain>0 )
    {
    // it means s1 contains at least one of searched strings
    }

    -----------------------------------------------------

    Hope that helps
    Please don't forget to up vote answers you like or which help you and mark one(s) which answer your question.





    • Marked as answer by sairam132033 Saturday, August 23, 2014 12:04 AM
    • Edited by DarjaGFX Saturday, August 23, 2014 12:08 AM
    Friday, August 22, 2014 11:32 PM

All replies

  • hi

    you can split your search phrase to 2 different characters and then do what you want like this:

    string s1 ="it is a nice day today";
    string searchString = "is;day";
    string [] ser = searchString.split(';');
    
    if ( s1.contains(ser[0]) && s1.contains(ser[1]))
    {
    // what you want do when both of them are in s1 string
    }
    if ( s1.contains(ser[0]) || s1.contains(ser[1]))
    {
    // what you want do when one of them are in s1 string
    }
    
    

    Friday, August 22, 2014 10:37 PM
  • Thanks. It works.

    But is there any way we can accomplish this using regular expression.

    Because sometime in the search string I will have n number of texts.

    for example:

    seachstring = s1;s2;s3;s4;s5;s6;s7;

    Friday, August 22, 2014 11:17 PM
  • if your search string type may change like "s1;s2;s3;s4;s5;s6;s7";

    then the Code should be like this :

    string s1 ="it is a nice day today";
    string searchString = "s1;s2;s3;s4;s5;s6;s7";
    string [] ser = searchString.split(';');
    
    // now ser can be like this: ser[0] = "blah1",
    // ser[1]="blah2" or like this : ser[0]= "blah1",
    //  ser[1] = "blah2" ser[3] = "blah3" and so on . . .
    
    int conain = 0;
    
    foreach (strign item in ser)
    {
    if (s1.contains(item))
    {
    contain ++ ;
    }
    }
    
    if(contain==ser.Getlength(0))
    {
    // it means s1 contains all of searched strings
    }
    
    
    if(contain==0)
    {
    // it means s1 contains non of searched strings
    }
    
    
    if(contain<ser.Getlength(0) && contain>0 )
    {
    // it means s1 contains at least one of searched strings
    }

    -----------------------------------------------------

    Hope that helps
    Please don't forget to up vote answers you like or which help you and mark one(s) which answer your question.





    • Marked as answer by sairam132033 Saturday, August 23, 2014 12:04 AM
    • Edited by DarjaGFX Saturday, August 23, 2014 12:08 AM
    Friday, August 22, 2014 11:32 PM