473,473 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Q: about Strings and chars.

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
Nov 15 '05 #1
4 1269
Sky wrote:
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.
Thanks,
Sky

Nov 15 '05 #2
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 :)
Nov 15 '05 #3
Sky <fo****@xact-solutions.com> wrote:
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).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
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 [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Sky <fo****@xact-solutions.com> wrote:
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).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Al Newton | last post by:
I want to use STL's sort algorithm to sort a string vector. Some of the strings are fairly long (300 to 400 chars) and the vector isn't small (5,000 to 10,000 elements). Naturally, sorting time...
17
by: DraguVaso | last post by:
Hi, I need to find the FASTEST way to get a string in a Loop, that goes from "a" to "ZZZZZZZZZZZZZZZZZ". So it has to go like this: a b .... z
5
by: Robert | last post by:
Hi, Is there some way of using an array of strings? Like in basic? I know you have to create an array of chars so i think it has to be an 2d array or something... Really stuck here... Thanks...
9
by: sci | last post by:
I believe both ways to create an array of strings are correct. Is there any difference between these two? 1. char *MyString = {"First string", "Second string", ..."Tenth string"}; 2. char...
7
by: Roman Mashak | last post by:
Hello, All! I wonder is it possible to define an array containing strings, not single characters? What I want is array 'table' that will have N elements, and every element is a strings tailoring...
19
by: pkirk25 | last post by:
I wonder if anyone has time to write a small example program based on this data or to critique my own effort? A file called Realm List.html contains the following data: Bladefist-Horde...
0
by: flameboy | last post by:
I am currently trying to figure my way how to output strings to a file. I have seen all the many posts and such how to do it in a console program, and have had much success doing it that way. This...
5
by: Alex | last post by:
Hi all - Is there a standard way to handle special chars in strings in dotnet? I'm using csharp and having two seperate problems... 1. Passing in args... I need to pass in an arg that...
2
by: bearophileHUGS | last post by:
Helmut Jarausch: Asking in comp.compression is a good starting point. My suggestions (sorry if they look a bit unsorted): it depends on what language you want to use, how much you want to...
6
by: Christoph Zwerschke | last post by:
In Python programs, you will quite frequently find code like the following for removing a certain prefix from a string: if url.startswith('http://'): url = url Similarly for stripping...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.