473,382 Members | 1,252 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Learning OOP - why is string.Join static, while string.Spit is not?

I'm working on learning .NET and I'm curious about the reason for
using static methods in some cases. Specifically, the string class has
a split and a join method, but only the join method is static.

Both methods return a new string which is based on another string. It
would seem that both should not be static.

string mystring = "one, two, three, four";
string[] newstring = mystring.Split(',');

why not:

string[] mystring = new string[] {"one", "two", "three", "four"};
string newstring = mystring.Join(',');

I'd like to understand the underlying reason.

Thanks,
Epson
Jul 21 '05 #1
2 2062
"Epson Barnett" <go****@grider.org> wrote...
I'm working on learning .NET and I'm curious about the reason for
using static methods in some cases. Specifically, the string class has
a split and a join method, but only the join method is static.

Both methods return a new string which is based on another string. It
would seem that both should not be static.

string mystring = "one, two, three, four";
string[] newstring = mystring.Split(',');

why not:

string[] mystring = new string[] {"one", "two", "three", "four"};
string newstring = mystring.Join(',');

I'd like to understand the underlying reason.


Well I'll suggest it is because in your join example mystring isn't an
object of type String. It's a string array.

Split acts upon a string, so it is a method of a string. Join acts on a
string array and therefore it doesn't require a string to be defined. If it
did it would be an arbitrary instance of string used only to call the
method.

Tom
Jul 21 '05 #2
Epson Barnett <go****@grider.org> wrote:
I'm working on learning .NET and I'm curious about the reason for
using static methods in some cases. Specifically, the string class has
a split and a join method, but only the join method is static.

Both methods return a new string which is based on another string. It
would seem that both should not be static.

string mystring = "one, two, three, four";
string[] newstring = mystring.Split(',');

why not:

string[] mystring = new string[] {"one", "two", "three", "four"};
string newstring = mystring.Join(',');

I'd like to understand the underlying reason.


mystring here isn't a string, it's a string array - so you couldn't
have an instance method in string which worked that way. The only
single string parameter is the string which is used between each
element. In theory you could therefore have:

string newstring = ",".Join (mystring);

but that's hardly natural.

The thing is, when you descibe Join, you naturally talk about it
working "on" a collection of strings. When you describe Split, you
naturally talk about it working "on" a single string - it's therefore
appropriate for Split to be an instance method, but Join not to be.

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

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

Similar topics

14
by: Bob | last post by:
I have a function that takes in a list of IDs (hundreds) as input parameter and needs to pass the data to another step as a comma delimited string. The source can easily create this list of IDs in...
17
by: Tom | last post by:
Is there such a thing as a CONTAINS for a string variable in VB.NET? For instance, I want to do something like the following: If strTest Contains ("A","B", "C") Then Debug.WriteLine("Found...
2
by: Epson Barnett | last post by:
I'm working on learning .NET and I'm curious about the reason for using static methods in some cases. Specifically, the string class has a split and a join method, but only the join method is...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
7
by: Malcolm | last post by:
This is a program to convert a text file to a C string. It is offered as a service to the comp.lang.c community. Originally I thought it would be a five minute job to program. In fact there are...
4
by: neptundancer | last post by:
Hi, to extend my skills, I am learning python. I have written small program which computes math expression like "1+2*sin(y^10)/cos(x*y)" and similar, so far only + - * / ^ sin con tan sqrt are...
12
by: Ed | last post by:
Hi All, The sample code below doesn't work, but if you take out the Question Mark character, "?" in front of the value "Print" of the "action" attribute, it does work! Does anyone here know...
9
by: gs | last post by:
is there any built in function or dotnet framework(version 2) to merge a generic list of string into one string with each element delimited by specified delimiting string? or do I have to roll...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.