Reputation: 3822
I'm looking to remove duplicate phrases from any give string.
Example:
My_First_Post_My_First_Post.htm
Would have the phrase "My_First_Post" in there twice, thus becoming:
My_First_Post_.htm
Any easy way to do this?
Upvotes: 3
Views: 137
Reputation: 224904
You can try regular expressions - careful about efficiency for sure, though:
Regex re = new Regex(@"(?<m>(.+))(.*?)\k<m>", RegexOptions.Compiled);
string str = "My_First_Post_My_First_Post.htm";
re.Replace(str, "$1$2"); // My_First_Post_.htm
It removes the first, longest, repeated sequence. To make it at least 10 characters, e.g., change the first group to:
(?<m>(.{10,}))
To restrict the distance between characters to 2, e.g., change the second group to:
(.{,2}?)
For 1 character, just put (.??)
.
Upvotes: 10