文本框删除前导和拖尾空格和空行更好的办法 [英] TextBox remove leading and tailing spaces and empty lines better way

查看:129
本文介绍了文本框删除前导和拖尾空格和空行更好的办法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文本框,其中用户可以输入一些文字。 目前我删除所有使用此code空间和空行:

 私人无效RemoveSpacesAndEmptyLines()
{
    textBox.Lines = textBox.Lines.Where(VAL =>!val.Trim()长度= 0).ToArray();
    textBox.Lines = textBox.Lines.Select(C => c.Trim())的ToArray()。
}
 

但它是可以做到的只有一个电话吗?
我需要有只有那些东西除了它的空间线条,并删除所有的空格。

解决方案
  

但它是可以做到的只有一个电话吗?

当然,因为你可以链其中,选择

  textBox.Lines
    。凡(VAL => val.Trim()长度!= 0)
    。选择(C => c.Trim())的ToArray()。
 

I have text box in which user may input some text. Currently I'm removing all spaces and empty lines using this code:

private void RemoveSpacesAndEmptyLines()
{
    textBox.Lines = textBox.Lines.Where(val => val.Trim().Length != 0).ToArray();
    textBox.Lines = textBox.Lines.Select(c => c.Trim()).ToArray();
}

But is it possible to do only one call?
I need to have only lines that have something except spaces in it and also remove all spaces.

解决方案

But is it possible to do only one call?

Sure, because you can chain the Where and the Select:

textBox.Lines
    .Where(val => val.Trim().Length != 0)
    .Select(c => c.Trim()).ToArray();

这篇关于文本框删除前导和拖尾空格和空行更好的办法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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