如何从字符串中保留所有常用单词? [英] How can I leave all common word from a string?

查看:58
本文介绍了如何从字符串中保留所有常用单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个类似的字符串



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



输出结果如下:str =a.LastUpdate,a.AgreementID,b.CustomerID



我的意思是普通词只会出现一次!这里a.AgreementID来了3次。

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.

推荐答案

更重要的是,你为什么要在第一时间创建它?

它是很难做到 - 如果你的字符串总是以SELECT开头并且在最后一个逗号后没有任何内容,那么它也不是太糟糕:只需剥离SELECT,将逗号分隔,然后丢弃重复项。然后把它们全部重新组合在一起。



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.

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...


您好,



单独使用列名称(删除SELECT一词)



尝试将您的字符串标记为以逗号为单独的标记数组。



您将获得



a.LastUpdate

a.AgreementID











那么你可以使用旧学校循环方法来保留第一个重复项。



然后将元素连接成一个逗号之间的字符串..



希望这会对你有所帮助。



问候,

C.Kumaresan

http://learnprogrammingeasily.blogspot.in/ [ ^ ]


试试这个

try this
var result = strname.Split(new[]{','}, StringSplitOptions.RemoveEmptyEntries).Distinct();


这篇关于如何从字符串中保留所有常用单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆