Noob
Noob

Reputation: 145

How to explicitly define adding space in a string?

In an effort to make my code more readable I'd like to specifically say that I am adding a space to a string instead of just saying + " ". As this could be easy confused with + "".

As a idea of what I'm looking for:

this.finalString = (string1 + //String.Space or Char.Space// +string2);

Is there a convention for this or is " " the proper way to do this?

Upvotes: 1

Views: 1002

Answers (2)

jmstoker
jmstoker

Reputation: 3475

To be honest anyone who is experienced in any language isn't going to confuse these two statements

this.finalString = (string1 + "" +string2);  // No one does this, unless it's a typo

and

this.finalString = (string1 + " " +string2);

Upvotes: 1

Rob
Rob

Reputation: 526

" " is the proper way to do this. :)

If you really need to be explicit, you can setup a constant to represent a single whitespace character.

If you don't like constants, you can create your own variables (depending on the language you're using):

String space = " ";
String return = Environment.NewLine;
String example = space + "word" + return + "anotherword";

Upvotes: 2

Related Questions