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? 9 2612
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
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];
}
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
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();
}
<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
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
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
<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();
}
}
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));
}
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: DrBob |
last post by:
gcc 3.3 MAC OS X.
I have a string that has trailing spaces in it that I want removed.
So i have a variable:
string x("abcd ");
x.trim() isn't an implemented method.
Is there a method I...
|
by: Simon |
last post by:
Hi,
I have written a function to trim char *, but I have been told that my way
could be dangerous and that I should use memmove(...) instead.
but I am not sure why my code could be 'dangerous'...
|
by: muroogan |
last post by:
I read a text file into a char variable
char tchar;
I want to trim leading spaces (Left trim).How can I do that in C++.
Any input will be appreciated.
|
by: Durgesh Sharma |
last post by:
Hi All,
Pleas help me .I am a starter as far as C Language is concerned .
How can i Right Trim all the white spaces of a very long (2000 chars)
Charecter string ( from the Right Side ) ? or how...
|
by: sengkok |
last post by:
I have develop a smart card device reading and writing program, but I am
facing a problem that when I read the value from the smart card, I get "A19\0
\0\0\0", (actually I have store the value A19...
|
by: Darren Anderson |
last post by:
I have a function that I've tried using in an if then statement and I've
found that no matter how much reworking I do with the code, the expected
result is incorrect.
the code:
If Not...
|
by: ucfcpegirl06 |
last post by:
Hello,
I have a dilemma. I am trying to flag duplicate messages received off
of a com port. I have a software tool that is supposed to detect dup
messages and flag and write the text "DUP" on...
|
by: rkk |
last post by:
Hi,
I've written a small trim function to trim away the whitespaces in a
given string. It works well with solaris forte cc compiler, but on
mingw/cygwin gcc it isn't. Here is the code:
char...
|
by: swengineer001 |
last post by:
Just looking for a few eyes on this code other than my own.
void TrimCString(char *str)
{
// Trim whitespace from beginning:
size_t i = 0;
size_t j;
while(isspace(str))
{
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |