【问题标题】:How to correctly clean spaces or enters from list of strings in WPF?如何正确清理空格或从 WPF 中的字符串列表中输入?
【发布时间】:2014-12-15 23:23:47
【问题描述】:

我正在创建这个简短的桌面应用程序,它可以清除多余的空格或从字符串中输入。您知道,有时当您从 pdf 复制文本以放置它时,例如在谷歌翻译器上比你粘贴,文本就像在带有额外输入或空格的行中刹车。所以我为我创建了这个简单的应用程序,它清理了这些多余的空间并将其输入并加入到一个段落中。

这是我调试错误的代码和评论:

List<string> content = new List<string>();
TextRange textRange = new TextRange(RichTb1.Document.ContentStart, RichTb1.Document.ContentEnd);
TextRange joiniText = new TextRange(RichTb2.Document.ContentStart, RichTb2.Document.ContentEnd);

string[] lines = textRange.Text.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
//to here is all ok, you can see in my List "lines" all lines that I have put it on RichTb1
content.AddRange(lines);

//this is just validation if entry in RichTb1 is empty (if not empty procede with action)
string match1 = content.ElementAt(0);

if (!string.IsNullOrWhiteSpace(match1))
{
   //**Here is problem, it clean all spaces or enters - empty lines, but also it clean not empty lines it also cleans some strings, see example down**
   content = content.Where(s => !string.IsNullOrWhiteSpace(s)).Distinct().ToList();

   joinText.Text = content.Aggregate((i, j) => i + " " + j);  
}

这是它所做的结果,例如你放了一些这样的随机文本:

"Chapter 4 illustrates the growing recognition
of
the
benefits
of
community
management
of
natural
resources.
To
ensure
that

such
approaches
do
not
exclude
poor
people,

**women,
the
elderly**
and
other
marginalized

groups,
governments
and
other
organizations

that
sponsor
community-based
projects
need

to
involve
all
groups
in
decision-making
and

implementation."

我的应用程序的结果是这样的:

"Chapter 4 illustrates the growing recognition of the benefits community management natural resources. To ensure that such approaches do not exclude poor people, **women, elderly** and other marginalized groups, governments organizations sponsor community-based projects need to involve all groups in decision-making implementation."

如您所见(这只是示例),它只是清除了一些不应该出现的单词,在上面的示例(强文本)中,您可以看到,单词 "the" 丢失了,在第一个文本中有这个单词。同样在我的台词中,我可以看到这个词。但是当行出现问题时,它会清除不应该出现的字符串(单词)。

任何想法是什么问题...在此先感谢

【问题讨论】:

  • DISTINCT 只允许返回不同的单词...
  • @MichaelMcGriff 感谢您的回复,您建议使用什么。
  • 只需删除不同的。为什么你认为这是必要的?
  • am :) 你能把它回答一下,这样我就可以投票了,这是一个解决方案......

标签: c# wpf string space


【解决方案1】:

即使它被接受,我也会建议一种不酷的方法。一个普通的StringBuilder 更高效、更简单:

StringBuilder sb = new StringBuilder(text.Length);
bool firstSpace = true;
char[] dont = { '\n', '\r' };
for(int i = 0; i < text.Length; i++)
{
    char c = text[i];
    if (dont.Contains(c)) c = ' ';  // replace new-line characters with a single space
    bool isWhiteSpace = Char.IsWhiteSpace(c) ;
    bool append =  !isWhiteSpace || firstSpace;
    firstSpace = !isWhiteSpace;
    if(append) sb.Append(c);
}
string withOneSpaceAndNoLines = sb.ToString();

【讨论】:

    【解决方案2】:

    DISTINCT 只允许返回不同的单词。只需将其删除,您就不会再有任何问题了。

    在此处查看 MSDN 文档:http://msdn.microsoft.com/en-us/library/system.linq.enumerable.distinct(v=vs.95).aspx

    【讨论】:

    • 感谢您的帮助和解释,我真的为问题道歉,我在某处找到了此代码行作为清理空白空间的解决方案...感谢您的帮助。
    • @krnekires 很高兴。以后要小心使用复制和粘贴的代码! :)
    • 我在这里找到了这一行:stackoverflow.com/questions/11867070/…我看到我没有仔细阅读它
    猜你喜欢
    • 2015-12-06
    • 2012-02-07
    • 1970-01-01
    • 2021-08-10
    • 2011-02-08
    • 2021-11-12
    • 2017-04-28
    • 2020-05-19
    • 2020-03-03
    相关资源
    最近更新 更多