ad,
First, you need to create a hashtable (or dictionary, I'll use a
hashtable since I don't know if you have access to .NET 1.1).
// Create the hashtable. If you want case-insensitivity, then you have to
use
// an overload of the constructor that takes the appropriate
case-insensitive
// comparer.
Hashtable parts = new Hashtable()
// Cycle through the parts.
foreach (string part in myString.Split(new char[1]{','}))
{
// If the part exists in the hashtable, don't add it.
if (!parts.ContainsKey(part))
{
// Add the part.
parts.Add(part, true);
}
}
// Now cycle through the parts and re-create the string.
// The builder for the new string.
StringBuilder builder = new StringBuilder();
// Cycle through the keys.
foreach (string key in parts.Keys)
{
// Append to the string builder.
parts.AddFormat("{0},", key);
}
// Remove the last comma.
if (parts.Count > 0) then
{
// Remove the last comma.
parts.Remove(builder.Length - 1, 1);
}
// The string.
myString = builder.ToString();
I didn't run this, so make sure you test it first.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
ad said:
I have a string like
string myString="dog,cat,dog,tiger"
I want to delete the duplication part, and make myString to
"dog,cat,tiger"
How can I do that?