Q: about Strings and chars.

S

Sky

Hi. Coming from PHP where all things are strings, the char is a bit hard to
get used to.

So far, I'm not really sure what the dif/use is between a string and a
char[] array...except notation, and the fact that most functions I expect to
use a string with, expect a char, or char array...

Specifically, I am now looking at the String.Split function that has to be
called as

string[] o = "The,Cat,Sleeps".split(",".ToCharArray());

What I am not sure about is Why it has to be converted to a char array. Most
languages I know you give it another string to split by, not a char array...
Is there a split function that accepts a string, btw?

I'm asking, not grumbling, to try to get a good handle on understanding the
reason why chars are more efficient, etc. and why to "love" em rather than
'hate' em. So far seem to get in the way rather than help.

Any pointers/frame of mind that anybody has to offer?
Thanks,
Sky
 
C

C# Learner

Sky said:
Hi. Coming from PHP where all things are strings, the char is a bit hard to
get used to.

So far, I'm not really sure what the dif/use is between a string and a
char[] array...except notation, and the fact that most functions I expect to
use a string with, expect a char, or char array...

Specifically, I am now looking at the String.Split function that has to be
called as

string[] o = "The,Cat,Sleeps".split(",".ToCharArray());

What I am not sure about is Why it has to be converted to a char array.

It's because you can tell the method to split on a series of different
characters.

e.g.:

string[] array = "The,Cat;Sleeps".Split(new char[] { ',', ';'});
Most
languages I know you give it another string to split by, not a char array...
Is there a split function that accepts a string, btw?

You'd have to use a Regex for that.

e.g.:

Regex r = new Regex("The.,Cat.,Sleeps");
string[] array = r.Split(".,");
I'm asking, not grumbling, to try to get a good handle on understanding the
reason why chars are more efficient, etc. and why to "love" em rather than
'hate' em. So far seem to get in the way rather than help.

Any pointers/frame of mind that anybody has to offer?

Think of strings as collections of characters that "flow", like in a
sentence. Think of character arrays as arrays of distinct, seperate
characters.
 
C

C# Learner

C# Learner wrote:

Regex r = new Regex("The.,Cat.,Sleeps");
string[] array = r.Split(".,");

Actually, I think the period would need to be escaped there. My lack of
regular expression knowledge is showing :)
 
J

Jon Skeet [C# MVP]

Sky said:
Hi. Coming from PHP where all things are strings, the char is a bit hard to
get used to.

So far, I'm not really sure what the dif/use is between a string and a
char[] array...except notation, and the fact that most functions I expect to
use a string with, expect a char, or char array...

A char array is mutable - a string isn't. That's the big difference
between the two, along with String obviously having various methods to
search, substring etc.
Specifically, I am now looking at the String.Split function that has to be
called as

string[] o = "The,Cat,Sleeps".split(",".ToCharArray());

No it doesn't. You can call it as:

string[] o = "The,Cat,Sleeps".Split(',');

for instance, as the parameter has the params modifier.
What I am not sure about is Why it has to be converted to a char array. Most
languages I know you give it another string to split by, not a char array...

String.Split splits on a set of delimiters, and each delimiter is a
char.
Is there a split function that accepts a string, btw?

You'd normally use a regular expression to split by a string rather
than a char (or set of chars).
 
S

Sky

Aha! That's the little ticket. The use of ' instead of " for chars!
Therefore:

tSrc = "The,Cat,ate,the,plate";
x = tSrc.split(",".toCharArray()); //wrong (or atleast long winded)
whereas:
x = tSrc.Split(','); is correct if the splitter is one char (tab, comma,
etc.);
but x = tSrc.Split('at,'); would split it to be:
"The","C","t","t","h"...etc.
because it is using each char of the chararray as a splitter...


but use Regex if splitting by the word "at"

Think I got it! Thanks.



Jon Skeet said:
Sky said:
Hi. Coming from PHP where all things are strings, the char is a bit hard to
get used to.

So far, I'm not really sure what the dif/use is between a string and a
char[] array...except notation, and the fact that most functions I expect to
use a string with, expect a char, or char array...

A char array is mutable - a string isn't. That's the big difference
between the two, along with String obviously having various methods to
search, substring etc.
Specifically, I am now looking at the String.Split function that has to be
called as

string[] o = "The,Cat,Sleeps".split(",".ToCharArray());

No it doesn't. You can call it as:

string[] o = "The,Cat,Sleeps".Split(',');

for instance, as the parameter has the params modifier.
What I am not sure about is Why it has to be converted to a char array. Most
languages I know you give it another string to split by, not a char
array...

String.Split splits on a set of delimiters, and each delimiter is a
char.
Is there a split function that accepts a string, btw?

You'd normally use a regular expression to split by a string rather
than a char (or set of chars).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top