.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

Get Distinct Values from string builder in c#

Posted By: gopal     Posted Date: March 30, 2012    Points:5   Category :C#
hi..

i want get distinct values from a string builder which contains duplicate values.

i want to achieve this without using for loop.

my sttring bulilder string would be like this:

StringBuilder sb = new StringBuilder();
sb.Append("1,1,2,3,4");

so my final output would be like this.

1,2,3,4

how to do that.

need ur suggetsions..

regards
gopal.s



Responses
Author: Mahesh             
Posted Date: March 30, 2012     Points: 20   

Hi,
try the code below,

static void Main(string[] args)
{

StringBuilder sr = new StringBuilder();
sr.Append("1,1,1,2,3");
Console.WriteLine(sr.ToString());
for (int i = 0; i < sr.Length; i++)
{
for (int j = i+1; j < sr.Length; j++)
{
if (sr[j] == sr[i] && (sr[i] != ',' || sr[j] != ','))
{
sr.Remove(j, 1);

}
}
}
for (int i = 0,j=i+1; j < sr.Length; i++,j++)
{
if (sr[j] == sr[i])
{
sr.Remove(j, 1);
i--;
j--;
}
}


Console.WriteLine(sr.ToString());

Console.ReadLine();
}


Hope this helps........

Regards,
Mahesh

Author: Narayanan             
Posted Date: May 10, 2012     Points: 20   

Hi Mahesh,
Nice code.

Regards, Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/
Author: Mohanraj             
Posted Date: July 05, 2012     Points: 20   

hi,

use the below code..

List<char> test=new List<char>();
StringBuilder str = new StringBuilder();
str.Append("1,1,2,3,4");
test = str.ToString().Distinct().ToList();
str = new StringBuilder();
foreach (char val in test)
{
if (val.ToString() != ",")
{
str.Append(val);
if (val != test[test.Count - 1])
str.Append(",");

}

}

Author: Sreeraj Nair Mungath             
Posted Date: July 05, 2012     Points: 20   

Its my code.
Mr Mohanraj copied it

List<char> test=new List<char>();
StringBuilder str = new StringBuilder();
str.Append("1,1,2,3,4");
test = str.ToString().Distinct().ToList();
str = new StringBuilder();
foreach (char val in test)
{
if (val.ToString() != ",")
{
str.Append(val);
if (val != test[test.Count - 1])
str.Append(",");

}

}



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend