Search This Blog

Tuesday, July 22, 2014

How can match particular word with in string.

How can match particular word with in string.

In below example we have room name string, we want categories our room as per category:
Deluxe,Single,Double etc. We have function roomCategory2()  and room name “Deluxe 1 Bedroom Suite Courtyard or City view” we can provide Deluxe category this room.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        roomCategory2();
    }
        
   //***********************************************************
    public string roomCategory()
    {
        string[] category = new string[] {"Deluxe","Single","Double"};

        string roomname = "Deluxe 1 Bedroom Suite Courtyard or City view";
        string retVal="";
        foreach (string finalname in category)
        {
             retVal = roomname.Split(" \n\r".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).First(p => p.Equals(finalname));
           
        }
        Response.Write(retVal);
       
        return "";
     }

//***********************************************************


    public string roomCategory1()
    {
        string[] category = new string[] { "Deluxe", "Single", "Double" };

        string roomname = "Deluxe 1 Bedroom Suite Courtyard or City view";
        string retVal = "";
      
        foreach (string x in category)
        {
            if (x.Contains(roomname))
            {
                Response.Write("true");
            }

            else
            {
                Response.Write("false");
            }
        }
       

        return "";
    }
//***********************************************************

    public string roomCategory2()
     {
        string[] category = new string[] { "Deluxe", "Single", "Double" };

        string roomname = "Single 1 Bedroom Suite Courtyard or City view";
        string[] splitroomname = roomname.Split(" \n\r".ToCharArray());
        string str="";
        foreach (var item in splitroomname)
        {
            foreach(var item1 in category)
              {
                  if (item == item1)
                      str = item1;
              }
           
        }
        Response.Write(str);
        return str;
       
     }
    //***********************************************************
  
}

1 comment :

Unknown said...

It was so nice article and useful to Informatica learners. we also provide Dotnet Course online training our Cubtraining is leader in providing Software Training