string s="00121234455" In this how to eliminate duplicates? using C# code [Resolved]

Posted by Asrinu under C# on 7/29/2010 | Views : 8659 | Status : [Member] | Replies : 7
string s="00121234455" In this how to eliminate duplicates? using C# code




Responses

Posted by: Neeks on: 7/30/2010 [Member] Bronze

Up
0
Down

Resolved
We do not require to create the List to manage the duplicate values.

We can simply do with the string variable...

string s = "Hello World";

string tempString = "";
foreach (char c in s)
{
//Append the character, If character is already in the string append the "" value else append the character
tempString += tempString.Contains(c.ToString()) ? "" : c.ToString();
}
MessageBox.Show(tempString);

//[OutPut: Helo Wrld]


Asrinu, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Poster on: 7/29/2010 [Member] Starter

Up
0
Down
You can write a small program with the loop and condition, something like

using System.Collections.Generics;


string str = "00121234455";
string temp = string.Empty;
List<string> list = new List<string>();
for (int i = 1; i <= str.length; i++)
{
temp = str.SubString(i-1, i); // get all the characters one by one
if (!list.Contains(temp))
{
list.Add(temp);
}
}

string distinctString = list.ToString();


Hope you would be able to refine this code, if needed.

Thanks



Asrinu, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: T.saravanan on: 7/30/2010 [Member] [MVP] Silver

Up
0
Down
string s="00121234455";
string sResult=string.Empty;
for(int i=0;i<s.length;i++)
{
if(!sResult.Contains(s(i).ToString()))
{
sResult += s(i).ToString();
}
}
return sResult;

I think its useful to you.
Thanks


Thanks,
T.Saravanan

Asrinu, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Poster on: 7/30/2010 [Member] Starter

Up
0
Down
Hey man, @T.saravanan solution might not work as "s" is not an array here. s is simply a string and you can not have an indexer in the string.

I am sure, my solution would work.



Asrinu, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Poster on: 7/30/2010 [Member] Starter

Up
0
Down
Very true Neeks, I am glad you have given the best answer.

I request Webmaster to mark it as Answered.

Asrinu, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Neeks on: 7/30/2010 [Member] Bronze

Up
0
Down
Thanks for the appreciation
One thing i would like to add in
1. T.saravanan
We are using the C# code and if you are using s(i), it is considered as function. So, in C# it should be s[i].

2. Poster
Do we require the substring for getting the Character from the string we can simply do with the character loop from the string.
like-
foreach(char c in strVariable)

{
//Do Work here...
}


Asrinu, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: T.saravanan on: 8/2/2010 [Member] [MVP] Silver

Up
0
Down
Hi Neeks,
You are right.Actually square brace is not working, so i am using normal brace.Now only i am known from your question, if we using 'i' in square brace it takes Italic model.

Thanks,

Thanks,
T.Saravanan

Asrinu, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response