Friday, November 1, 2013

c# - List strings in order of occurrence in a given string

In the project that I am working on right now I had to figure out a way to list a given set of strings in another string based on the order in which the list of strings would occur.

For example: I have a few raw html page content extracted from the web and I have a list of words.The task is to build a grid display that would list those words in order of occurrence in the content of a page. 

Below is the sample code using a console to achieve it.If there is a better way to do this, I am interested to know.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StringOrder

{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //Set of random words to search for in Content
                string[] saArrayofStrings = { "abc", "def", "mno", "xyz", "jkl" };
                //Content
                string sString = "xyz The history of the United States as covered in American schools and universities typically begins with either Christopher Columbus's 1492 voyage to the Americas or with the prehistory of the Native peoples, with the latter approach having become increasingly common in recent decades.[1]jkl Indigenous peoples lived in what is now the United States for thousands of years and developed complex cultures before European colonists began to arrive, mostly from England, after 1600. The Spanish had early settlements in Florida and the Southwest, and the French along the Mississippi River and Gulf Coast.abc By the 1770s, thirteen British colonies contained two and a half million people along the Atlantic coast, east of the Appalachian Mountains.mno The colonies were prosperous and growing rapidly, and had developed their own autonomous political and legal systems. However, with the end of the French and Indian War in 1763, Great Britain altered its relationships with the colonies by imposing tighter administrative controls and greater financial obligations on the colonists [2]. Tensions grew, eventually leading to armed conflict beginning in April 1775. def On July 4, 1776, the colonies declared independence from the Kingdom of Great Britain.";
                SortedList<int, string> OrderList = new SortedList<int, string>();
                foreach (String s in saArrayofStrings)
                {
                    if (sString.Contains(s))
                        OrderList.Add(sString.IndexOf(s), s);
                }
                Console.WriteLine("The order of occurrence of the given list of words in the content is below");
                foreach (KeyValuePair<int, string> pair in OrderList)
                    Console.WriteLine(pair.Value + " occurs at the following position:" + pair.Key.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }
    }
}

Output:





No comments: