Regex
is your friend!
using System;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace ConsoleApplication16
{
class Program
{
static readonly string[] Tests = { "abcdabcd", "xabcdabcd", "abcdabc", "xaaabcdabcd" };
static readonly Regex FindDup = new Regex(@"(.+)\1", RegexOptions.IgnoreCase);
static void Main(string[] args)
{
foreach (string t in Tests)
{
MatchCollection allMatches = FindDup.Matches(t);
Trace.WriteLine(string.Format("{0}: {1}", t, allMatches.Count));
}
}
}
}
Results:
abcdabcd: 1
xabcdabcd: 1
abcdabc: 0
xaaabcdabcd: 2
This will also identify what the matching strings are, in the
allMatches
collection.
EDIT: Add response to Collin's comments:
Each of the
Match
values in the
allMatches
contains the information about the doubled text in the
Groups
property.
Groups[0]
contains the whole matched string (both copies), and
Groups[1]
contains the string of the single copy.
If you change the loop above to:
foreach (string t in Tests)
{
MatchCollection allMatches = FindDup.Matches(t);
Trace.WriteLine(string.Format("{0}: {1}", t, allMatches.Count));
foreach (Match item in allMatches)
{
Trace.WriteLine(string.Format(@" ""{0}"" is doubled", item.Groups[1]));
}
}
You'll see that.
If the objective is to
remove the duplications, then use of
Replace()
method of the Regex will do the job:
string t2 = FindDup.Replace(t, string.Empty);
Trace.WriteLine(string.Format(@"Final: ""{0}""", t2));
Of course, a different string can be substituted in instead of
string.Empty