locked
Check if a string contains a word or not RRS feed

  • Question

  • User1632102518 posted

    I have 2 strings,from which I want check if the string contains the complete word "part" instead of partner etc...

    Input Strings:

    event_adm-event_call-part

    event_adm-event_call-partner_transaction

    Output  Strings:

    event_adm-event_call-part

    When I do  if (Test.Contains("-part")){} it is returning true for  both the values but I need only the string that contains -part .

    Wednesday, November 4, 2015 6:18 PM

Answers

  • User2103319870 posted

    sreeja1234

    When I do  if (Test.Contains("-part")){} it is returning true for  both the values but I need only the string that contains -part .

    You can check with Regex for a particular word in a string like below

                    //Check if string has word Part
    //Change the test string as per your input
    if(Regex.IsMatch(test, @"\bpart\b")) { //String contains Part } else { //String does not contains Part }

    You can see a sample working demo here

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 4, 2015 6:25 PM

All replies

  • User2103319870 posted

    sreeja1234

    When I do  if (Test.Contains("-part")){} it is returning true for  both the values but I need only the string that contains -part .

    You can check with Regex for a particular word in a string like below

                    //Check if string has word Part
    //Change the test string as per your input
    if(Regex.IsMatch(test, @"\bpart\b")) { //String contains Part } else { //String does not contains Part }

    You can see a sample working demo here

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 4, 2015 6:25 PM
  • User-1716253493 posted
            string str = "event_adm-event_call-part";
            Boolean b = false;
            foreach (string s in str.Split('_'))
            {
                if(s.EndsWith("-part"))
                {
                    b = true;
                }
            }
            if (b==true)
            {
    
            }

    Wednesday, November 4, 2015 7:17 PM