我想使用 .net 删除 Windows 窗体应用程序中文本框中的任何前导空格
不允许文本框后的第一个字符作为空格允许在 Windows 窗体应用程序中使用空格 喜欢
textbox1.text=" fg"; //----------- not like
textbox1.text="vcvc hfh"; //------------Like this
not allowing first character as spaces after textbox allow spaces in windows form application
like
textbox1.text=" fg"; //----------- not like
textbox1.text="vcvc hfh"; //------------Like this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以将此代码放在
TextChanged
事件或OnKeyDown
事件中(或者您可以随时使用它),甚至可以直接进入要点
You could place this code in the
TextChanged
event orOnKeyDown
event (or you could use it whenever you want)or even straight to the point
这将避免文本框开头出现空格
链接
This will avoid space at the start of the textbox1
Link
您还可以使用正则表达式通过用空字符串 ("") 替换前导空格来删除前导空格。在此示例中,“Hello”将变为“Hello”:
同样,您可以去除尾随空格。在此示例中,“Hello”将变为“Hello”:
如果使用管道运算符(“|”),则可以在同一表达式中给出两种模式以去除前导和尾随空格。这是我在 C# 项目中使用的特定版本。它在 .NET 中相当于 Perl chomp 命令。在此示例中,“Hello”将变为“Hello”:
有关 .NET 正则表达式的大量非常精彩的教程、示例和参考信息,请参阅此站点:
注意:要在 C# 中使用正则表达式,必须添加
using System. Text.RegularExpressions;
到 C# 文件的顶部。另请注意:如果您愿意,可以用
[ \t\r\n]
替换[\s]
空格运算符。You could also use a regular expression to remove leading white space by substituting the leading white space with an empty string (""). In this example, " Hello " would become "Hello ":
Similarly, you could strip trailing white space. In this example, " Hello " would become " Hello":
If you use the pipe operator ( "|" ), you can give both patterns in the same expression to strip both leading and trailing white space. This is the particular one I use in my C# project. It is the .NET equivalent to the Perl chomp command. In this example, " Hello " would become "Hello":
For a slew of really great tutorial, examples, and reference information on .NET regular expressions, see this site:
Note: To use regular expressions in C#, you must add
using System.Text.RegularExpressions;
to the top of your C# file.Note also: If you prefer, you can substitute
[ \t\r\n]
for the[\s]
white space operator.当您准备好使用文本时,为什么不直接 Trim() 文本呢?
Why not just Trim() the text when you're ready to use it?
删除文本中的所有空格,即使是在开头、中间或结尾,
这是我的代码
To Remove All Space in your Text Even in Start, Middle or End
Here's my Code
您可以测试一下按下的键是否是空格键,或者修剪它。也许。
You could test to see if the key down is the spacebar, or trim it. Perhaps.