2

I'm just wondering whether .Net has a built-in enum of chars anywhere.

I'm just parsing a CSV file and it occured to me I'd like to be able to do something like:

line.Split(Chars.Comma)

instead of:

line.Split(char(','))

It just seems cleaner to me.

Does .Net have this tucked away anywhere?

Ps: I know Windows.Forms.Keys does in most situations, but doesn't have all the punctuation symbols.

Edit (after most of the answers):

I should have mentioned that I'm coding in Boo, not C# (hence why I asked for .Net in general), so line.Split(',') doesn't work as ',' is a string in Boo.

I hadn't taken into account it would have to cover unicode, which would be massive, whereas all I was planning on using it for are to save me from having to create constants for punctuation symbols every time I create an app which does that kind of text manipulation.

Here's the way I chose to solve this:

static class StingSplitters:

    public final Comma = char(',')
    public final BackSlash = char('\\')
    ...

Thanks for all the input.

andyhasit
  • 14,137
  • 7
  • 49
  • 51
  • 1
    your second code sample should be `line.Split(',')`, which I find the cleanest of them all – Cristian Lupascu Mar 13 '13 at 10:08
  • 1
    however, for parsing CSV I recommend using a library, such as [File Helpers](http://filehelpers.sourceforge.net/) – Cristian Lupascu Mar 13 '13 at 10:09
  • There are 65536 characters available in C#; each of them has an existing character representation... `'a'`, `'b'`, etc... (ok, some are a little harder to represent). There is no need for an *enum*, but there *is* something in what you say... not for the `char`, but for the `char[]`. – Marc Gravell Mar 13 '13 at 10:14
  • @w0lf: I have to use Split(char(',')) because I'm coding in Boo, not c#, so ',' would be a string. – andyhasit Mar 13 '13 at 10:20

5 Answers5

3

No, there isn't such a thing. To what end, really? Should there actually be a class containing a constant for every Unicode character, being kept in sync with Unicode whenever they assign new code points? And I'd guess that names like

Chars.ArabicLigatureUighurKirghizYehWithHamzaAboveWithAlefMaksuraIsolatedForm

get a little unwieldy compared to 'ﯹ'.

You can also write

line.Split(',');

which is not really harder to read than line.Split(Chars.Comma).

Joey
  • 344,408
  • 85
  • 689
  • 683
  • `char` ***is*** an integral type; it is basically `ushort` – Marc Gravell Mar 13 '13 at 10:15
  • in what way is it *not* an integral type, in your view? – Marc Gravell Mar 13 '13 at 10:17
  • hint: here's the C# language reference of "integral types": http://msdn.microsoft.com/en-gb/library/exx3b86w(v=vs.110).aspx – Marc Gravell Mar 13 '13 at 10:18
  • Ok, rephrasing: For the purpose of `enum`s you cannot use it as they are restricted in what types they can derive from. May we now get past one instance of poor choice of words in the morning and back to the point? – Joey Mar 13 '13 at 10:19
  • Thanks, hadn't considered the unicode implications, though IMO an ASCII only set wouldn't be that unreasonable a thing to include in the framework... – andyhasit Mar 13 '13 at 11:09
  • Marc has a point with char arrays you might need (though the performance impact is likely negligible for 99 % of the use cases). But I still don't see why anyone would need character constants when literals do exactly the same thing. – Joey Mar 13 '13 at 11:33
1

You can just do:

line.Split(',');

Or make a const:

const char COMMA = ',';
...
line.Split(COMMA);

Or make with enum:

 public enum Chars {
    Comma = ',',
 }

 line.Split((char)Chars.Comma);

In my opinion the const solution is the cleanest. However I would not call my const COMMA, but call it SEPERATOR. That is what is really is.

Peter
  • 27,590
  • 8
  • 64
  • 84
1
line.Split(',');

works just fine. However, note that this is actually a params array and involves a new array each time. I advise creating a static char[] for the things you use most commonly. You could wrap this in a fancy wrapper, but I'd just keep it simple. I genuinely have a class that is:

// interesting fact; we have hundreds of locations where we use someString.Split(char) or similar; every single one
// of those allocates a new array; idea: fill this in and do a mass replace to avoid a shit-ton of allocations
public static class StringSplits
{
    public static readonly char[] Space = { ' ' },
                                  Comma = { ',' },
                                  Period = { '.' },
                                  ...
                                  NewLine_CarriageReturn = { '\n', '\r' },
                                  Comma_SemiColon = { ',', ';' },
                                  Comma_SemiColon_Space = { ',', ';', ' ' },
                                  BackSlash_Slash_Period = { '\\', '/', '.' };

Note that some of these are not single-character.

It sounds daft, but if you are doing lots of string manipulation, it can add up to a lot of unnecessary arrays.

Frankly, I would love .NET 5 to include methods like:

string.Split(char);
string.Split(char,char);

instead of just:

string.Split(params char[] chars);
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • The `Console.Write` methods each have overloads for up to three parameters, interestingly. – Joey Mar 13 '13 at 10:17
  • Seems you had the same unease as me about creating new chars (or actually params arrays as you point out) every time. I was actually heading towards a class like yours but wondering whether I was reinventing the wheel. – andyhasit Mar 13 '13 at 11:05
0

Windows.Forms.Keys only needs to know about what is displayed on your keyboard. A char enum would need a lot more values since it's unicode and can contain up to ~65.000 different values. Just imagine you type Char. and get a dropdown with 65k possible options.

So no, there is no Char enum. If you want a constant, you need to define one yourself.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
0

First of all , the name of class is Char not Chars in .net. Second thing, i think there is no enum for the chars, you should pass a character array for more than one characters as the delimiters.

for details you can see this page. http://msdn.microsoft.com/en-us/library/system.string.split.aspx

Sarim Javaid Khan
  • 810
  • 15
  • 30