1

我正在尝试删除某些字符。并修剪列表中的项目。这是我的问题的简单说明。

private void add_Click(object sender, EventArgs e)
{
    string[] s = { "Tarik \"F\"", "Tarik   ", "Tarik", "Souad  ", "Mehdi FARID", "Souad F  ", "DAFIR CCF", "khalid FA", "SAFAF" };

    char[] c = { 'F', ' ', '"' };
    foreach (string b in s)
    {
        txtDisplay.AppendText(b.TrimEnd(c) + "\n");
    }
}

结果是:

Tarik,
Tarik,
Tarik,
Souad,
Mehdi FARID,
Souad,
DAFIR CC: (for this item I want to keep the last 'F'),
SAFA    : (for this item I want to keep the last 'F')

这个想法是,如果 char 'F' 是单词的一部分,它不能被删除,如果是孤立的,那么是的。

4

3 回答 3

1

使用正则表达式:

        var regex = new Regex(@"\W+[Ff]*(?=\W|$)");
        foreach (var s in strings)
        {
            txtDisplay.AppendText(regex.Replace(s, "") + Environment.NewLine); 
        }

无论这个人有多少名字,这个解决方案都有效。

于 2013-09-02T01:22:13.830 回答
0

这对我有用:

var results = s.Select(x =>
    String.Join(" ", x
        .Split(new [] { '"', ' ',})
        .Where(z => z != "F")));

我得到这些结果:

结果

于 2013-09-02T03:08:28.220 回答
0
 string[] s =
             {
               "Tarik \"F\"", "Tarik   ", "Tarik", "Souad  ", "Mehdi FARID", "Souad F  ", "DAFIR CCF",
                    "khalid FA", "SAFAF"
             };
 char[] c = {  ' ', '"' };
 txtDisplay.AppendText(s.Select(s1 => s1.Split(c)).Select(l => l.Where(s2 => 
                                    !String.IsNullOrWhiteSpace(s2) && !s2.Equals("F"))
                                    .Aggregate("", (current, s2) => current + 
                                    (current.Length > 0 ? " " + s2 : s2))).Aggregate(
                                    "", (current1, temp) => current1 + (temp + "\n")));
于 2013-09-02T03:49:24.920 回答