修剪方法不会清除所有空间
您好,我正在使用 C# 开发一个基本的 Windows 窗体,但 Trim() 方法有一些问题。 有 3 个文本框,用户可以在其中输入他的名字、姓氏和 ID。 然后他可以通过单击保存按钮来保存信息,但我想确保他不会留下空白框,所以我做了以下测试:
string CFN = Curator_FN.Text;
string CLN = CURATOR_LN.Text;
string CID = CURATOR_ID.Text;
Curator_FN.Text.Trim();
CURATOR_ID.Text.Trim();
CURATOR_LN.Text.Trim();
if (((Curator_FN.Text.Length == 0) || (CURATOR_ID.Text.Length == 0) || (CURATOR_LN.Text.Length == 0)))
{
MessageBox.Show("You Have to enter a First Name, a Last Name and an ID");
Empty = true;
}
问题是如果我只是用空格键创建一些空白 Trim() 方法不会将它们视为空格。 也许我只是误解了 Trim() 方法,如果我这样做了,你知道我该如何做到这一点吗? 提前致谢。
Hi i'm working on a basic windows form in c# and I have a little problem with the Trim() method.
There are 3 text boxes in witch the user enters his first name, last name and an ID.
Then he can save the info by clicking on a save button but I want to make sure that he doesn't leave blank boxes so I do the following test:
string CFN = Curator_FN.Text;
string CLN = CURATOR_LN.Text;
string CID = CURATOR_ID.Text;
Curator_FN.Text.Trim();
CURATOR_ID.Text.Trim();
CURATOR_LN.Text.Trim();
if (((Curator_FN.Text.Length == 0) || (CURATOR_ID.Text.Length == 0) || (CURATOR_LN.Text.Length == 0)))
{
MessageBox.Show("You Have to enter a First Name, a Last Name and an ID");
Empty = true;
}
The problem is if I just make some blank space with the space bar the Trim() method doesn't consider them as a blank space..
Maybe I just misunderstand the Trim() method and if I do, do you have any idea on how I could do the this?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Trim
方法不会修改文本框的内容,它只是返回修剪后的版本。例如,您需要存储此版本当然这有可能使更改对用户可见(并且它还必须访问 UI 线程,这在其他情况下可能会出现问题),因此最好使用局部变量当然,
如果这是您需要做的全部,请使用
string.IsNullOrWhiteSpace
可能是一个更方便的替代方案。The
Trim
method does not modify the contents of the text boxes, it simply returns the trimmed version. You need to store this version, for exampleOf course this has the potential to make changes visible to the user (and it also has to access the UI thread which under other circumstances might be a problem), so it is far better to use a local variable as in
Of course if this is all you need to do, using
string.IsNullOrWhiteSpace
might be a more convenient alternative.由于字符串在 C# 中是不可变的,因此 Trim() 方法不会更改字符串本身;它返回修剪字符串的新实例。
您需要将方法调用的结果分配给变量,即
,然后检查CFN是否为空。
Since strings are immutable in C#, the Trim() method doesn't change the string itself; it returns a new instance of the trimmed string.
You need to assign the results of the method calls to the variables, i.e.
And then check whether or not CFN is empty.
Trim 不会修改字符串。您想要:
另外,如果您使用 .NET 4,您可能需要检查 String.IsNullOrWhiteSpace 方法也是如此。
Trim does not modify the string. You want:
Also, if you're using .NET 4 you might want to check into the String.IsNullOrWhiteSpace method as well.
Trim 不会修改字符串本身。它返回一个新的修剪字符串。
如果您并不真正关心修改变量,请查看 IsNullOrWhiteSpace 方法。
Trim does not modify the string itself. It returns a new trimmed string.
If you don't really care about modifying the variable, look at the IsNullOrWhiteSpace method.