1

I have a prompt asking user to enter a date (in a specific format). Then I trim the string, however if the there was an extra space when I hit 'Enter' in the prompt box, the string still comes out with an extra space after the trim. I'll post my prompt box code too. My string is: Jul 29, 2015 1:32:01 PM PDT and Jul 30, 2015 12:34:27 PM PDT

    string afterpromptvalue = Prompt.ShowDialog("Enter earliest Date and Time", "Unshipped Orders");
                    afterpromptvalue.Trim();
                    string beforepromptvalue = Prompt.ShowDialog("Enter latest Date and Time", "Unshipped Orders");
                    beforepromptvalue.Trim();

 string format = "MMM dd, yyyy h:mm:ss tt PDT";

            CultureInfo provider = CultureInfo.InvariantCulture;

            afterpromptvalue.Trim();
            beforepromptvalue.Trim();


            DateTime createdAfter = DateTime.ParseExact(afterpromptvalue, format, provider);



            DateTime createdBefore = DateTime.ParseExact(beforepromptvalue, format, provider);

public static class Prompt
{
    public static string ShowDialog(string text, string caption)
    {
        Form prompt = new Form();
        prompt.Width = 500;
        prompt.Height = 150;
        prompt.FormBorderStyle = FormBorderStyle.FixedDialog;
        prompt.Text = caption;
        prompt.StartPosition = FormStartPosition.CenterScreen;
        Label textLabel = new Label() { Left = 50, Top=20, Text=text };
        TextBox textBox = new TextBox() { Left = 50, Top=50, Width=400 };
        Button confirmation = new Button() { Text = "Ok", Left=350, Width=100, Top=70, DialogResult = DialogResult.OK };
        confirmation.Click += (sender, e) => { prompt.Close(); };
        prompt.Controls.Add(textBox);
        prompt.Controls.Add(confirmation);
        prompt.Controls.Add(textLabel);
        prompt.AcceptButton = confirmation;

        return prompt.ShowDialog() == DialogResult.OK ? textBox.Text : "";
    }
}
danny taki
  • 450
  • 7
  • 26
  • The trim method returns a string you are aware of that right ? – Bongo Jul 30 '15 at 20:50
  • possible duplicate of [Why does \`String.Trim()\` not trim the object itself?](http://stackoverflow.com/questions/10190818/why-does-string-trim-not-trim-the-object-itself) – Bongo Jul 30 '15 at 21:30

3 Answers3

13

string.Trim returns a new string. It does not update the existing variable.

Strings are immutable--the contents of a string object cannot be changed after the object is created

https://msdn.microsoft.com/en-us/library/362314fe.aspx

The proper syntax for your code would be:

afterpromptvalue = afterpromptvalue.Trim();

Adam V
  • 6,256
  • 3
  • 40
  • 52
2

Calling Trim() on a string doesn't change the string itself. It returns the string trimmed.

In C#, strings aren't changeable. All you can do to a string is assign a new string to it, calling methods on a string can not change the original string unless you assign the return value to the string object in question.

For example, change:

afterpromptvalue.Trim();

To:

afterpromptvalue = afterpromptvalue.Trim();
Jakotheshadows
  • 1,485
  • 2
  • 13
  • 24
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
1

You should try this maybe ;)

afterpromptvalue = afterpromptvalue.Trim();
beforepromptvalue = beforepromptvalue.Trim();

And read this :

https://msdn.microsoft.com/en-gb/library/d4tt83f9(v=VS.110).aspx

Maybe this quote from here is interesting for you

Immutability and the StringBuilder class

A String object is called immutable (read-only), because its value cannot be modified after it has been created. Methods that appear to modify a String object actually return a new String object that contains the modification.

Community
  • 1
  • 1
Bongo
  • 2,933
  • 5
  • 36
  • 67