0

With both string.Format and using string concatenation is adding spaces in my string, which is in turn making my Test methods fail. Here is my ToString() method:

public override string ToString()
    {
        return string.Format("First name: {0}, Last name: {1}, Age: {2}", FirstName, LastName, Age);

    }//end of ToString()

I also tried this and got the same output:

public override string ToString()
    {
        return "First name: " + FirstName  + ", Last name: " + LastName + ", Age: " + Age;

    }//end of ToString() 

There is no space after the place holder, before the comma but when run this is the output:

First name: Jen , Last name: Doe , Age: 26

It puts a space after the placeholder, Why is this? how do I stop it

1
  • Are you sure that this is not simply a case of all the names having an extra space at end? Commented Jan 31, 2015 at 5:07

1 Answer 1

1
FirstName.Trim()

The variables might contain spaces.

Sign up to request clarification or add additional context in comments.

1 Comment

You know I was already using Trim() so at first I thought "no way I am trimming the whitespace, but then ... I looked at the implicit operator and realized when looping over the string it was adding the whitespace. duh?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.