We need to combine 3 columns in a database by concatenation. However, the 3 columns may contain overlapping parts and the parts should not be duplicated. For example,
<
I'm trying to make this C# as pleasant to read as possible.
public static string Concatenate(string s1, string s2)
{
if (string.IsNullOrEmpty(s1)) return s2;
if (string.IsNullOrEmpty(s2)) return s1;
if (s1.Contains(s2)) return s1;
if (s2.Contains(s1)) return s2;
char endChar = s1.ToCharArray().Last();
char startChar = s2.ToCharArray().First();
int s1FirstIndexOfStartChar = s1.IndexOf(startChar);
int overlapLength = s1.Length - s1FirstIndexOfStartChar;
while (overlapLength >= 0 && s1FirstIndexOfStartChar >=0)
{
if (CheckOverlap(s1, s2, overlapLength))
{
return s1 + s2.Substring(overlapLength);
}
s1FirstIndexOfStartChar =
s1.IndexOf(startChar, s1FirstIndexOfStartChar);
overlapLength = s1.Length - s1FirstIndexOfStartChar;
}
return s1 + s2;
}
private static bool CheckOverlap(string s1, string s2, int overlapLength)
{
if (overlapLength <= 0)
return false;
if (s1.Substring(s1.Length - overlapLength) ==
s2.Substring(0, overlapLength))
return true;
return false;
}
EDIT: I see that this is almost the same as jerryjvl's solution. The only difference is that this will work with the "abcde", "d" case.