Friday, October 08, 2004 4:08 PM jerdenn

Can we please add support for String overloads wherever we see char[] in the framework?

Aaahhh!

One of my pet peeves, actually, is that sprinkled throughout the framework are many places that insist on taking a char[].  The instance method String.Split() is one such example, though I am certain that you will see many others. 

Why, oh why didn't we overload these methods to take a String?  It's not like I keep a char[] in my pocket, just waiting to use.  I've got to dust off a Char.Parse(), or some such ugly construct.

The String is one of the most natural objects in the framework.  Microsoft really got something right here, with a unified string type, instead of the mess of BSTR and LPSTR and char* and CString and wchar and TCHAR and LPCTSTR and basic_string and (breath, Jerry, breath!!)...

And, I'm not even mentioning the custom strings that people write for C++.  I think I would run out of blog space here.

So, since the String class is one of the most "natural" types in .NET, a string parameter should be the type of choice when passing charactor data.   Does anyone really have a good reason to offer a char[] param, and not a corresponding String overload?

Filed under:

Comments

# re: Can we please add support for String overloads wherever we see char[] in the framework?

Friday, October 08, 2004 5:47 PM by Weston Weems

ehh... I understand its a nuisance, but its not like its hard to fix.

Mystring.Split("'".ToCharArray());

# re: Can we please add support for String overloads wherever we see char[] in the framework?

Friday, October 08, 2004 7:00 PM by Jim Hughes

By definition, a string is zero, one OR MORE characters, in many cases, a function that requires only a single character makes sense.

It is not hard to make a string a char using "a"c

where the c suffix states that the aforementioend string literail is in fact a single character.

# re: Can we please add support for String overloads wherever we see char[] in the framework?

Friday, October 08, 2004 8:13 PM by Jerry Dennany

Jim, I wasn't speaking of a char, I was speaking of char[], which would potentially indicate the presence of more than one charactor.

If the string was empty or null, then the behavior would be the same as if the char[] was null...

I understand that it is easy to overcome, but the fact that I have to litter my code with unneccessary conversions is annoying.

# re: Can we please add support for String overloads wherever we see char[] in the framework?

Friday, October 08, 2004 10:09 PM by AndrewSeven

Its because it wont split on a string, only on the various chars in the array.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemstringclasssplittopic1.asp


I just found a post about splitting strings on strings... RegEx.Split

http://weblogs.asp.net/justin_rogers/archive/2004/03/14/89545.aspx

# re: Can we please add support for String overloads wherever we see char[] in the framework?

Friday, October 08, 2004 10:40 PM by Justin Rogers

Couple of notes. I wrote a pretty nice string splitting function if you follow the link posted above. Very fast, customizable for your own situation, and very memory conservative.

With that said, there is a String.Split(string[]...) in Whidbey. It splits based on any number of sub-strings. It does not appear to be written quite as well as it could, but given that it is in the BCL, I imagine it will get a lot more usage than anything I might write and toss up on my blog.

Leave a Comment

(required) 
(required) 
(optional)
(required)