By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,215 Members | 1,920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,215 IT Pros & Developers. It's quick & easy.

How to trim the duplicate char in a string

P: n/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?
Nov 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
ad <ad@wfes.tcc.edu.tw> wrote:
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).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2

P: n/a
dd
ad wrote:
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[i] != str[i-1])
nstr += str[i];
}
Nov 17 '05 #3

P: n/a
dd <ne*****@here.com> wrote:
ad wrote:
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[i] != str[i-1])
nstr += str[i];
}


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4

P: n/a
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();

}

Nov 17 '05 #5

P: n/a
<al*******@users.com> wrote:
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*.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6

P: n/a
ad
Thanks,
Could you check it when usr input with TextBox by a
RegularExrpessionValidation?
"Jon Skeet [C# MVP]" <sk***@pobox.com>
???????:MP***********************@msnews.microsoft .com...
<al*******@users.com> wrote:
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*.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #7

P: n/a
ad <ad@wfes.tcc.edu.tw> wrote:
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".

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8

P: n/a
<al*******@users.com> wrote:
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();
}
}
Nov 17 '05 #9

P: n/a
I wrote:
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));
}
}
Nov 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.