我想使用 .net 删除 Windows 窗体应用程序中文本框中的任何前导空格

发布于 2024-11-07 03:41:02 字数 190 浏览 1 评论 0原文

不允许文本框后的第一个字符作为空格允许在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

赠意 2024-11-14 03:41:02

您可以将此代码放在 TextChanged 事件或 OnKeyDown 事件中(或者您可以随时使用它)

string myString = textBox1.Text.TrimStart()

,甚至可以直接进入要点

textBox1.Text = textBox1.Text.TrimStart()

You could place this code in the TextChanged event or OnKeyDown event (or you could use it whenever you want)

string myString = textBox1.Text.TrimStart()

or even straight to the point

textBox1.Text = textBox1.Text.TrimStart()
若有似无的小暗淡 2024-11-14 03:41:02

这将避免文本框开头出现空格

void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
     if ((sender as TextBox).SelectionStart == 0)
          e.Handled = (e.KeyChar == (char)Keys.Space);
     else
          e.Handled = false;
}

链接

This will avoid space at the start of the textbox1

void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
     if ((sender as TextBox).SelectionStart == 0)
          e.Handled = (e.KeyChar == (char)Keys.Space);
     else
          e.Handled = false;
}

Link

故人爱我别走 2024-11-14 03:41:02

您还可以使用正则表达式通过用空字符串 ("") 替换前导空格来删除前导空格。在此示例中,“Hello”将变为“Hello”:

String result = Regex.Replace(textbox1.text, "^[ \t\r\n]", "");

同样,您可以去除尾随空格。在此示例中,“Hello”将变为“Hello”:

String result = Regex.Replace(textbox1.text, "[ \t\r\n]+$", "");

如果使用管道运算符(“|”),则可以在同一表达式中给出两种模式以去除前导和尾随空格。这是我在 C# 项目中使用的特定版本。它在 .NET 中相当于 Perl chomp 命令。在此示例中,“Hello”将变为“Hello”:

String result = Regex.Replace(textbox1.text, "^[ \t\r\n]+|[ \t\r\n]+$", "");

有关 .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 ":

String result = Regex.Replace(textbox1.text, "^[ \t\r\n]", "");

Similarly, you could strip trailing white space. In this example, " Hello " would become " Hello":

String result = Regex.Replace(textbox1.text, "[ \t\r\n]+$", "");

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":

String result = Regex.Replace(textbox1.text, "^[ \t\r\n]+|[ \t\r\n]+$", "");

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.

別甾虛僞 2024-11-14 03:41:02

当您准备好使用文本时,为什么不直接 Trim() 文本呢?

Why not just Trim() the text when you're ready to use it?

踏月而来 2024-11-14 03:41:02

删除文本中的所有空格,即使是在开头、中间或结尾,

这是我的代码

using System.Text.RegularExpressions;

txtAfter.Text = Regex.Replace(txtBefore.Text, " ", "");

To Remove All Space in your Text Even in Start, Middle or End

Here's my Code

using System.Text.RegularExpressions;

txtAfter.Text = Regex.Replace(txtBefore.Text, " ", "");
ㄟ。诗瑗 2024-11-14 03:41:02

您可以测试一下按下的键是否是空格键,或者修剪它。也许。

You could test to see if the key down is the spacebar, or trim it. Perhaps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文