Click here to Skip to main content
15,905,144 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Suppose I have a string like that

string str ="a.LastUpdate,a.AgreementID,a.AgreementID,b.CustomerID,a.AgreementID"

the output will be like: str="a.LastUpdate,a.AgreementID,b.CustomerID"

I mean common word will come only one time! Here a.AgreementID has come 3 times.
Posted
Updated 27-Feb-14 0:13am
v3

More to the point, why are you creating it in the first place?
It's quite hard to do - if your string always starts with SELECT and has nothing after the last comma, then it's not too bad: just strip off the SELECT, split the remainder on the comma, and throw away the duplicates. Then just put it all back together.

C#
string str = "SELECT a.LastUpdate,a.AgreementID,a.AgreementID,b.CustomerID,a.AgreementID,";
string justTheList = str.Substring(7);
string[] elements = justTheList.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
str = "SELECT " + string.Join(",", elements.Distinct());


But...it'd be a lot tidier to do it when you generate the string in the first place...
 
Share this answer
 
Comments
sachi Dash 27-Feb-14 6:11am    
nice suggestion ... but it does not work !

ok you think like that.... then give me code for this one.

string str ="a.LastUpdate,a.AgreementID,a.AgreementID,b.CustomerID,a.AgreementID"

the output will be like: str="a.LastUpdate,a.AgreementID,b.CustomerID"
OriginalGriff 27-Feb-14 6:24am    
As I said:
"if your string always starts with SELECT and has nothing after the last comma"
CPallini 27-Feb-14 6:13am    
More compact than mine.
OriginalGriff 27-Feb-14 6:25am    
I like Distinct - it's a handy little devil! :laugh:
Hi,

Take the column names alone(remove the word "SELECT")

Try to tokenize your string into an array of tokens with comma as separaters.

You will get

"a.LastUpdate"
"a.AgreementID"
.
.
.
.

Then you can use old school for loop method to eliminate duplicates by keeping the first one.

Then concatenate the elements into a string with comma in between..

Hope this helps you.

Regards,
C.Kumaresan
http://learnprogrammingeasily.blogspot.in/[^]
 
Share this answer
 
Comments
OriginalGriff 27-Feb-14 6:05am    
Edit your answer and remove the link: it will be counted as spam, and your account could be removed as a result.
try this
var result = strname.Split(new[]{','}, StringSplitOptions.RemoveEmptyEntries).Distinct();
 
Share this answer
 
You might:
  1. Remove the 'irregular' part of the string (namely "SELECT ").
  2. Split the string into a string array using the comma as separator.
  3. Add all the array items to a dictionary.
  4. Build again the string concatenating "SELECT " and all the dictionary keys.

(step 3 removes duplicate entries)
 
Share this answer
 
Of course, you can use LINQ to implement steps 2 and 3 of CPallini's solution:
C#
string str ="a.LastUpdate,a.AgreementID,a.AgreementID,b.CustomerID,a.AgreementID";
string noDuplicates = string.Concat(str.Split(',').Distinct());
 
Share this answer
 
HI,

Try this,

C#
var str = ("a.LastUpdate,a.AgreementID,a.AgreementID,b.CustomerID,a.AgreementID").Split(',');
var res = str.Distinct();
 
Share this answer
 
v2
Comments
agent_kruger 28-Feb-14 6:31am    
sir, this can be done in one line.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900