Core C#

Pattern Matching on a String

This C# program demonstrates how to program a pattern match on a string in C#.

Program.cs

using System;
using System.Collections.Generic;

namespace Strings {
    public class CIndexOf {
        static public void Example1() {
            string sSource = "Hear, O heavens, and give ear, O earth;\n" +
                "\tfor the Lord has spoken:\n" +
            "'Sons have I reared and brought up,\n" +
                "\tbut they have rebelled against me.\n" +
            "The ox knows its owner,\n" +
            "but Israel does not know,\n" +
                "\tmy people does not understand.'\n" +

            "Ah, sinful nation,\n" +
                "\ta people laden with iniquity,\n" +
             "offspring of evildoers,\n" +
                "\tsons who deal corruptly!\n" +
             "They have forsaken the Lord,\n" +
                "\tthey have despised the Holy One of Israel,\n" +
                "\tthey are utterly estranged.\n"+
                "\t\t--(Isaiah 1:1-4)";

            Console.WriteLine("This is the string that we will perform searches on:{0}{0}\"{1}\"{0}", Environment.NewLine, sSource);

            string sTarget = "";
            List<int> qLocations = new List<int>();

            do {
                // Remove the previous search results and reset the number of 
                qLocations.Clear();
                int iTotalHits = 0;
                Console.Write("Type a text pattern and hit Enter to search the string or nothing to end: ");

                sTarget = Console.ReadLine();

                if (sTarget != "") {
                    // Loop until there are no more patterns located
                    for (int iCurrLoc = 0; iCurrLoc < sSource.Length; iCurrLoc++) {
                        iCurrLoc = sSource.IndexOf(sTarget, iCurrLoc);
                        // -1 is returned when nothing is found
                        if (iCurrLoc >= 0) {
                            qLocations.Add(iCurrLoc);
                            iTotalHits++;
                        } else
                            break;
                    }
                } else
                    return;

                Console.WriteLine("{0}The search parameter '{1}' was found {2} times.{0}",
                        Environment.NewLine, sTarget, iTotalHits);

                Console.Write("The string was fount at: ");
                foreach (int iLoc in qLocations) {
                    Console.Write("{0}  ", iLoc);
                }
                Console.WriteLine();

            } while (true);
        }
    }
}
 

Output

This is the string that we will perform searches on:

"Hear, O heavens, and give ear, O earth;
        for the Lord has spoken:
'Sons have I reared and brought up,
        but they have rebelled against me.
The ox knows its owner,
        and the ass its master's crib;
but Israel does not know,
        my people does not understand.'
Ah, sinful nation,
        a people laden with iniquity,
offspring of evildoers,
        sons who deal corruptly!
They have forsaken the Lord,
        they have despised the Holy One of Israel,
        they are utterly estranged.
                --(Isaiah 1:1-4)"

Type a text pattern and hit Enter to search the string or nothing to end: ear

The search parameter 'ear' was found 4 times.

The string was fount at: 1  26  33  80
Type a text pattern and hit Enter to search the string or nothing to end: they

The search parameter 'they' was found 3 times.

The string was fount at: 107  383  427
Type a text pattern and hit Enter to search the string or nothing to end:
Press any key to continue . . .
 
 

© 2007–2025 XoaX.net LLC. All rights reserved.