Thread: how to return startindex of basestring as soon as it contains the substring value

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    4

    how to return startindex of basestring as soon as it contains the substring value

    Code:
      public static int StringMatch(string baseStr, string compStr)        {
              
                int count = 0;
                int index = 0;
                for (int i = 0; i < baseStr.Length; i++)
                {
    
    
                    for (int j = 0; j < compStr.Length; j++)
                    {
    
    
                        if (baseStr[i]==compStr[j])
                        {
    
    
                            index = i; // i need to return indexof the first character of basestring
                            count++;
                        }
    
    
                        if(count==compStr.Length)
                        {
                            
                            Console.WriteLine("string match");
                        }
                    }
    
    
                }
               return index;
    
    
    
    
            }
    
    
            static void Main(string[] args)
            {
    
    
                string txt = "fftf kargi kargi";
                string phrase = "kargi";
                int res = StringMatch(txt, phrase);
                Console.WriteLine(res);
            }
        }
    }

  2. #2
    Registered User
    Join Date
    May 2016
    Posts
    4
    Code:
       public static int StringMatch(string baseStr, string compStr)        {
                //bool b = false;
                //int count = 0;
                int index = 0;
                for (int i = 0; i < baseStr.Length; i++)
                {
    
    
                    for (int j = 0; j < compStr.Length; j++)
                    {
    
    
                        if (baseStr[i] == compStr[j] && baseStr[i] == compStr[compStr.Length - 1])
                        {
    
    
                            Console.WriteLine("string match");
                        }
                    }
    
    
                }
                return index;
    
    
            }
    
    
            static void Main(string[] args)
            {
    
    
                string txt = "fftfgood    good         good good good good";
                string phrase = "good";
                int res = StringMatch(txt, phrase);
                Console.WriteLine(res);
            }
        }
    }

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Seems like you're doing everything the hard way:
    Code:
            using System.Text.RegularExpressions;
    
            Regex r = new Regex(phrase);
            Match m = r.Match(txt);
            if (m.Success) {
                Console.WriteLine(m.Index); 
            }
    Last edited by whiteflags; 05-23-2016 at 01:42 PM.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I'd avoid a regex match just because the phrase might unintentionally end up with regex special characters and skew the results.

    There is a really easy way to do this in C# though:

    Code:
    int res = txt.IndexOf(phrase);
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. substring in C
    By -EquinoX- in forum C Programming
    Replies: 2
    Last Post: 04-18-2008, 01:39 PM
  2. Substring Help (Simple?)
    By heynowletsgo in forum C Programming
    Replies: 17
    Last Post: 10-22-2007, 05:58 AM
  3. Getting a substring help plz
    By joshua in forum C Programming
    Replies: 3
    Last Post: 11-01-2005, 06:11 PM
  4. substring error
    By ghe1 in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 11:35 AM
  5. How to substring ?
    By appsforum in forum C Programming
    Replies: 1
    Last Post: 09-10-2001, 03:22 PM

Tags for this Thread