Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternatives to " " for creating strings containing multiple whitespace characters

Tags:

string

c#

spaces

I'm wondering if there's a more OO way of creating spaces in C#.

Literally Space Code!

I currently have tabs += new String(" "); and I can't help but feel that this is somewhat reminiscent of using "" instead of String.Empty.

What can I use to create spaces that isn't " "?

like image 627
Jamie Dixon Avatar asked Nov 11 '09 16:11

Jamie Dixon


People also ask

What are the different types of whitespace characters?

Space, tab, line feed (newline), carriage return, form feed, and vertical tab characters are called "white-space characters" because they serve the same purpose as the spaces between words and lines on a printed page — they make reading easier.

Which function used string with white spaces?

isspace() in C/C++ and its application to count whitespace characters.

How do you whitespace a character?

With many keyboard layouts, a whitespace character may be entered by pressing spacebar . Horizontal whitespace may also be entered on many keyboards with the Tab ↹ key, although the length of the space may vary.

How do I remove a whitespace character from a string?

The replaceAll() method of the String class replaces each substring of this string that matches the given regular expression with the given replacement. You can remove white spaces from a string by replacing " " with "".


2 Answers

You can write

" " 

instead of

new String(' ') 

Does that help?


Depending on what you do, you might want to look into the StringBuilder.Append overload that accepts a character and a 'repeat' count:

var tabs = new StringBuilder(); tabs.Append(' ', 8); 

or into the string constructor that constructs a string from a character a 'repeat' count:

var tabs = new string(' ', 8); 

Here's an enterprisey OO solution to satisfy all your space generation needs:

public abstract class SpaceFactory {     public static readonly SpaceFactory Space = new SpaceFactoryImpl();      public static readonly SpaceFactory ZeroWidth = new ZeroWidthFactoryImpl();      protected SpaceFactory { }      public abstract char GetSpace();      public virtual string GetSpaces(int count)     {         return new string(this.GetSpace(), count);     }      private class SpaceFactoryImpl : SpaceFactory     {         public override char GetSpace()         {             return '\u0020';         }     }      private class ZeroWidthFactoryImpl : SpaceFactory     {         public override char GetSpace()         {             return '\u200B';         }     } } 
like image 198
dtb Avatar answered Oct 06 '22 19:10

dtb


Now that you've clarified in comments:

In my actual code I'm doing new String(' ',numberOfSpaces) so I probably need to still use the new String part.

... the other answers so far are effectively useless :(

You could write:

const char Space = ' '; 

then use

new string(Space, numberOfSpaces) 

but I don't see any benefit of that over

new string(' ', numberOfSpaces) 
like image 42
Jon Skeet Avatar answered Oct 06 '22 18:10

Jon Skeet