How to trim the duplicate char in a string

  • Thread starter Thread starter ad
  • Start date Start date
A

ad

I have a string like "1,2,2,3,3,3,4"
I want to trim off the duplicate part, and make it to "1,2,3,4"
How can I do?
 
ad said:
I have a string like "1,2,2,3,3,3,4"
I want to trim off the duplicate part, and make it to "1,2,3,4"
How can I do?

The best thing to do would be to split the values (e.g. with
String.Split) then rebuild the string, checking for duplicates as you
go (possibly using a Hashtable to remember which ones you've already
seen).
 
ad said:
I have a string like "1,2,2,3,3,3,4"
I want to trim off the duplicate part, and make it to "1,2,3,4"
How can I do?
Try with this snippet

string nstr = "";
for (int i = 1;i< str.Length;i++)
{
if (str != str[i-1])
nstr += str;
}
 
Try with this snippet

string nstr = "";
for (int i = 1;i< str.Length;i++)
{
if (str != str[i-1])
nstr += str;
}


Not only is that inefficient (in terms of using string concatenation
unnecessarily - you should use a StringBuilder for looped
concatenation), it also doesn't do what's required. If you look at the
test string, in no case is one character followed by the same
character. It would work to *some* extent if you changed the i-1 to
i-2, but then it would still fail if either the duplicate came in a
non-adjacent position, or if the values were more than one character
wide.
 
I'd probably go with a mix of better ideas from the above
- use Split to parse the input string
- use StringBuilder to generate the new string


string RemoveDuplicates (
string inStr
)
{

const string SEP = ",";

System.Text.StringBuilder retBuilder = new
System.Text.StringBuilder();

string[] values= inStr.Split(SEP.ToCharArray());

string prev = string.Empty;

for (int Idx = 0; Idx < values.Length; Idx++) {
if (!(values[Idx].Equals(prev))) {
retBuilder.Append(values[Idx] + SEP);
prev=values[Idx];
}

}

if (retBuilder.Length > 0) {
int nLen = retBuilder.Length;
retBuilder = retBuilder.Remove(nLen-SEP.Length,SEP.Length);
}

return retBuilder.ToString();

}
 
I'd probably go with a mix of better ideas from the above
- use Split to parse the input string
- use StringBuilder to generate the new string

<snip>

Your code still only copes if the duplicate values come one after
another. That's why I'd suggest using a map of some description (eg
Hasthable) to find whether there are duplicates *anywhere*.
 
ad said:
Could you check it when usr input with TextBox by a
RegularExrpessionValidation?

I don't know enough about regular expressions to know whether that's
possible, but even if it is I suspect it would be less readable than
doing it "manually".
 
I'd probably go with a mix of better ideas from the above
- use Split to parse the input string
- use StringBuilder to generate the new string

[snip]

As Jon has said, your solution will only work if the values are
consecutive. I'd suggest the following (uncommented, for the sake of
briefness) solution instead.

using System;
using System.Collections;
using System.Text;

sealed class EntryPoint
{
EntryPoint() {
}

static void Main() {
Console.WriteLine(
MiscUtility.RemoveDuplicates("1,1,2,3,3,3,4", ','));
Console.Read();
}
}

sealed class MiscUtility
{
MiscUtility() {
}

public static string RemoveDuplicates(string input, char separator) {
string[] substrings = input.Split(separator);

UniqueStringList uniqueStringList = new UniqueStringList();
foreach (string substring in substrings) {
uniqueStringList.Add(substring);
}

StringBuilder sb = new StringBuilder();
foreach (string item in uniqueStringList) {
sb.Append(item + separator);
}
sb.Remove(sb.Length - 1, 1);

return sb.ToString();
}
}

sealed class UniqueStringList
{
ArrayList arrayList = new ArrayList();

public void Add(string item) {
if (!arrayList.Contains(item)) {
arrayList.Add(item);
}
}

public IEnumerator GetEnumerator() {
return arrayList.GetEnumerator();
}
}
 
I said:
I'd suggest the following (uncommented, for the sake of briefness)
solution instead.

[snip]

On second thoughts, this could be better:

using System;
using System.Collections;

sealed class EntryPoint
{
EntryPoint() {
}

static void Main() {
Console.WriteLine(
MiscUtility.RemoveDuplicates("1,1,2,3,3,3,4", ','));
Console.Read();
}
}

sealed class MiscUtility
{
MiscUtility() {
}

public static string RemoveDuplicates(string input, char separator) {
string[] substrings = input.Split(separator);
UniqueStringList uniqueStringList =
new UniqueStringList(substrings);
return String.Join(
separator.ToString(), uniqueStringList.ToArray());
}
}

sealed class UniqueStringList
{
ArrayList arrayList = new ArrayList();

public UniqueStringList(string[] arr) {
foreach (string element in arr) {
if (!arrayList.Contains(element)) {
arrayList.Add(element);
}
}
}

public string[] ToArray() {
return (string[])arrayList.ToArray(typeof(string));
}
}
 
Back
Top