Removes duplicate character strings (adjoining replication) in a string. - CSharp System

CSharp examples for System:String Strip

Description

Removes duplicate character strings (adjoining replication) in a string.

Demo Code

//  Copyright ? 2011, Grid Protection Alliance.  All Rights Reserved.
using System.Text.RegularExpressions;
using System.Text;
using System.IO;//  w ww . j  av a 2s .c om
using System.Globalization;
using System.ComponentModel;
using System.Collections.Generic;
using System;

public class Main{
        /// <summary>
        /// Removes duplicate character strings (adjoining replication) in a string.
        /// </summary>
        /// <param name="value">Input string.</param>
        /// <param name="duplicatedValue">String whose duplicates are to be removed.</param>
        /// <returns>Returns <paramref name="value" /> with all duplicated <paramref name="duplicatedValue" /> removed.</returns>
        public static string RemoveDuplicates(this string value, string duplicatedValue)
        {
            if (string.IsNullOrEmpty(value))
                return "";
            if (string.IsNullOrEmpty(duplicatedValue))
                return value;

            string duplicate = duplicatedValue + duplicatedValue;

            while (value.IndexOf(duplicate) > -1)
            {
                value = value.Replace(duplicate, duplicatedValue);
            }

            return value;
        }
}

Related Tutorials