Finding the Word in the Given Paragraph in Windows Store app

In this article I will discuss how to find the word in the give paragraph in Windows Store apps.

Introduction

In this article I discuss the Match class of the System.Text.RegularExpression namespace in Windows 8 application. By using this class we can find a particular word, value or letter that matches the given context. By using the Success property of the Match class we can determine whether the word matches or not; true indicates success. Their are various other properties that we can use in the Match class. The Match class represents the results from a single regular expression match. Some of the properties that I use in this application are :

  • Value : Gets the captured substring from the input string.

  • Index : The position in the original string where the first character of the captured substring is found.

  • Length : Gets the length of the captured substring.

  • Success : Gets a value indicating whether the match is successful.


Now to see how we find the word in the given paragraph, use the following steps :

Step 1

Open Visual Studio 2012 RC and click File -> New -> Project.

Step 2

A New Project dialog appears, in this select window Metro style inside the Visual C# at the left side pane then select Blank Page in the center pane and after giving the name of your application click ok.

Step 3

Now to do the designing by writing the code in the MainPage.xaml file as:

<Page

    x:Class="practice1.MainPage"

    IsTabStop="false"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:practice1"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d">

    <Grid>

        <Grid.Background>

            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

                <GradientStop Color="Black"/>

                <GradientStop Color="#FFBFD37D" Offset="1"/>

            </LinearGradientBrush>

        </Grid.Background>

        <TextBlock  Name="name" HorizontalAlignment="Left" Margin="91,133,0,0" TextWrapping="Wrap" Text="Enter Paragraph" VerticalAlignment="Top" Height="48" Width="269" FontSize="20"/>

        <TextBox  Name="paragraph" HorizontalAlignment="Left" Margin="402,78,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="135" Width="479"/>

        <TextBlock  x:Name="name_Copy" HorizontalAlignment="Left" Margin="91,257,0,0" TextWrapping="Wrap" Text="Word Search" VerticalAlignment="Top" Height="46" Width="269" FontSize="20"/>

        <TextBox Name="word" HorizontalAlignment="Left" Margin="402,257,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="46" Width="479"/>

        <TextBlock  x:Name="Display" HorizontalAlignment="Left" Margin="82,424,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="126" Width="817" FontSize="20"/>

        <Button Content="Found" HorizontalAlignment="Left" Margin="280,354,0,0" VerticalAlignment="Top" Height="42" Width="153" Click="Button_Click_1"/>

    </Grid>

</Page>

Step 4

Now add the namespace in the MainPage.xaml.cs file as:

using System.Text.RegularExpressions;

Step 5

Write the code in the MainPage.xaml.cs file as:
 

    public sealed partial class MainPage : Page

    {

        public MainPage()

        {

            this.InitializeComponent();

        }      

        protected override void OnNavigatedTo(NavigationEventArgs e)

        {

        }

        public void Button_Click_1(object sender, RoutedEventArgs e)

        {

            string pattern = word.Text;

            string input = paragraph.Text;

            Match match = Regex.Match(input, pattern);

            if (match.Success)

                // Report position as a one-based integer.

 

                Display.Text = "The word :" + match.Value + "\n was found  at position : " + (match.Index + 1) + "\n in \n" + input + "\n The length of the captured word is :" + pattern.Length;

 

            else

                Display.Text = "The Word : " + pattern + " \n was not found in : " + input;

        }

    }

Step 6

Now to Run the application, press F5. The output will look like:

Output-Screen-For-Finding-The-Word-In-Windows-8-Apps.jpg

Now enter the paragraph and the word that we want to search for and click on the find button. Now the details of that word would be displayed, like:

Finding-The-Word-In-Given-Paragraph-In-Windows-8-Apps.jpg

Summary

In this article I explained how to find the data in the given context.