Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I find matched string from 2 string (From the back). Example :

string string1 = "TodayisSaturday";
string string2 = "YesterdayisSaturday";


or

string string1 = "TodayisSaturday";
string string2 = "HowisSaturday";


How can I get "isSaturday" provided string 2 can be shorter than string 1 anytime, vice versa.
Posted
Comments
Prasad Avunoori 24-Jul-14 23:56pm    
Should it be case sensitive?

1 solution

Assumed both string are case sensitive.

C#
static string FindMatchString()
       {
           string string1 = "TodayisSaturday";
           string string2 = "HowisSaturday";

           string smallString = string1.Length > string2.Length ? string2 : string1;
           string bigString = string1.Length > string2.Length ? string1 : string2;

           string resultString = "";

           int smallStringLen = smallString.Length;
           int bigStringLen = bigString.Length;

           for (int i = smallStringLen - 1; i >= 0; i--)
           {

               if (smallString[i] == bigString[bigStringLen - 1])
               {
                   resultString = resultString + smallString[i].ToString();
               }
               bigStringLen--;
           }

           char[] arr = resultString.ToCharArray();

           Array.Reverse(arr);
           return new string(arr);


       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900