C # filter strings to keep only one

Source: Internet
Author: User

Method 1: Original Ecology: Call filterRepeatChar ("Zhu Yajun, Yao yier, tu Jing, tu Jing, Liu Gang, Yao yier ")

Public static string filterRepeatChar (string _ val)
{
String _ str = string. Empty;
String [] strArray = _ val. Split (',');
For (int I = 0; I <strArray. Length; I ++)
{
For (int j = I + 1; j <strArray. Length; j ++)
{
If (strArray [j] = strArray [I])
{
StrArray [I] = string. Empty;
}
}
If (strArray [I]! = String. Empty)
_ Str + = strArray [I] + ",";
}
If (_ str. LastIndexOf (",")>-1)
_ Str = _ str. Substring (0, _ str. Length-1 );
Return _ str;
}

 

Method 2: Use linq to implement filterRepeatCharToLinq ("Zhu Yajun, Yao yier, tu Jing, tu Jing, Liu Gang, Yao yier s ")

Public static string filterRepeatCharToLinq (string _ val)
{

Var wq = from p in _ val. Split (','). Distinct ()
Select p;

String rw = "";
Foreach (var kk in wq)
{
Rw + = kk. ToString () + ",";
}
Rw = rw. TrimEnd (',');

Return rw;

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.