修剪方法不会清除所有空间

发布于 2024-12-19 22:51:40 字数 733 浏览 1 评论 0原文

您好,我正在使用 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 技术交流群。

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

发布评论

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

评论(4

灼疼热情 2024-12-26 22:51:40

Trim 方法不会修改文本框的内容,它只是返回修剪后的版本。例如,您需要存储此版本

Curator_FN.Text = Curator_FN.Text.Trim(); 

当然这有可能使更改对用户可见(并且它还必须访问 UI 线程,这在其他情况下可能会出现问题),因此最好使用局部变量当然,

var curatorFn = Curator_FN.Text.Trim(); 
// etc

if (curatorFn.Length == 0 || ... ) {
    // show messagebox
}

如果这是您需要做的全部,请使用 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 example

Curator_FN.Text = Curator_FN.Text.Trim(); 

Of 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

var curatorFn = Curator_FN.Text.Trim(); 
// etc

if (curatorFn.Length == 0 || ... ) {
    // show messagebox
}

Of course if this is all you need to do, using string.IsNullOrWhiteSpace might be a more convenient alternative.

风情万种。 2024-12-26 22:51:40

由于字符串在 C# 中是不可变的,因此 Trim() 方法不会更改字符串本身;它返回修剪字符串的新实例。

您需要将方法调用的结果分配给变量,即

CFN = Curator_FN.Text.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.

CFN = Curator_FN.Text.Trim()

And then check whether or not CFN is empty.

哆兒滾 2024-12-26 22:51:40

Trim 不会修改字符串。您想要:

Curator_FN.Text = Curator_FN.Text.Trim();
CURATOR_ID.Text = CURATOR_ID.Text.Trim();
CURATOR_LN.Text = CURATOR_LN.Text.Trim();

另外,如果您使用 .NET 4,您可能需要检查 String.IsNullOrWhiteSpace 方法也是如此。

if (String.IsNullOrWhiteSpace(Curator_FN.Text) ||
    String.IsNullOrWhiteSpace(CURATOR_ID.Text) ||
    String.IsNullOrWhiteSpace(CURATOR_LN.Text)
{
  //..
}

Trim does not modify the string. You want:

Curator_FN.Text = Curator_FN.Text.Trim();
CURATOR_ID.Text = CURATOR_ID.Text.Trim();
CURATOR_LN.Text = CURATOR_LN.Text.Trim();

Also, if you're using .NET 4 you might want to check into the String.IsNullOrWhiteSpace method as well.

if (String.IsNullOrWhiteSpace(Curator_FN.Text) ||
    String.IsNullOrWhiteSpace(CURATOR_ID.Text) ||
    String.IsNullOrWhiteSpace(CURATOR_LN.Text)
{
  //..
}
像极了他 2024-12-26 22:51:40

Trim 不会修改字符串本身。它返回一个新的修剪字符串。

如果您并不真正关心修改变量,请查看 IsNullOrWhiteSpace 方法。

if (String.IsNullOrWhiteSpace(curatorFn) || ... ) {
    // show messagebox
}

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.

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