Split

Z

Zach

Using blabla.Split(whatever) you can split up a text-line into its
constituent elements of an array (as you will know well), my question:
is there code to do the opposite, say after you have nullified one of
the elements of the array? Please give simple example, no reference or
lecture.

Many thanks,
Zach.
 
Z

Zach

Using blabla.Split(whatever) you can split up a text-line into its
constituent elements of an array (as you will know well), my question:
is there code to do the opposite, say after you have nullified one of
the elements of the array? Please give simple example, no reference or
lecture.

Many thanks,
Zach.
PS I am aware that you can stick the elements together in a loop but
that is not what I am after.

Zach
 
A

Andy O'Neill

Zach said:
PS I am aware that you can stick the elements together in a loop but that
is not what I am after.

Zach

On the face of it, iterating through your array or collection of words and
concatenating non-null entries onto a stringbuilder in a loop would be the
way to do it.
What's the problem with that?
 
Z

Zach

On the face of it, iterating through your array or collection of words
and concatenating non-null entries onto a stringbuilder in a loop would
be the way to do it.
What's the problem with that?

Well, there is nothing wrong with that, true, but given the Split
option, I thought there might be an opposite of Split. Because you don't
need Split, to split up a string, code not so complicated to do that
either :)

Zach
 
P

Peter Duniho

Steve said:
It sounds like you are looking for string.Join(token, items []);

You know, it amazes me just what is hiding away in the .Net libraries.
Fantastic, really.

Though, I'm not sure I would call a public method in the System.String
class, documented along with every other public method in the
System.String class, quite "hidden". :)

The OP might benefit from some practice simply reviewing the
documentation for the classes he is working with.

Pete
 
A

Andy O'Neill

Peter Duniho said:
Steve said:
It sounds like you are looking for string.Join(token, items []);

You know, it amazes me just what is hiding away in the .Net libraries.
Fantastic, really.

Though, I'm not sure I would call a public method in the System.String
class, documented along with every other public method in the
System.String class, quite "hidden". :)

The OP might benefit from some practice simply reviewing the documentation
for the classes he is working with.

Pete

What happens if one of the entries in an array you use join on is null?
 
F

Family Tree Mike

Peter Duniho said:
Steve said:
It sounds like you are looking for string.Join(token, items []);

You know, it amazes me just what is hiding away in the .Net
libraries. Fantastic, really.

Though, I'm not sure I would call a public method in the System.String
class, documented along with every other public method in the
System.String class, quite "hidden". :)

The OP might benefit from some practice simply reviewing the
documentation for the classes he is working with.

Pete

What happens if one of the entries in an array you use join on is null?

It works fine:

string[] items = new string[] { "Red", "Green", null, "Blue" };
string output = string.Join(",", items);

produces: Red,Green,,Blue
 
A

Andy O'Neill

What happens if one of the entries in an array you use join on is null?

It works fine:

string[] items = new string[] { "Red", "Green", null, "Blue" };
string output = string.Join(",", items);

produces: Red,Green,,Blue

2 separators.
Depends on what output is required for a null entry.
 
F

Family Tree Mike

What happens if one of the entries in an array you use join on is null?

It works fine:

string[] items = new string[] { "Red", "Green", null, "Blue" };
string output = string.Join(",", items);

produces: Red,Green,,Blue

2 separators.
Depends on what output is required for a null entry.

Yes, whether it works, depends on your definition of works. :)

What I meant was, it produces a string, that when run through
string.split(), faithfully reproduces the input list of strings.
 
F

Family Tree Mike

What happens if one of the entries in an array you use join on is null?

It works fine:

string[] items = new string[] { "Red", "Green", null, "Blue" };
string output = string.Join(",", items);

produces: Red,Green,,Blue

2 separators.
Depends on what output is required for a null entry.

// Looks like my last sent message errored out on the server.

Yes, whether it works, depends on your definition of works. :)

What I meant was, it produces a string, that when run through
string.split(), faithfully reproduces the input list of strings.
 
A

Arne Vajhøj

Family Tree Mike said:
What happens if one of the entries in an array you use join on is null?

It works fine:

string[] items = new string[] { "Red", "Green", null, "Blue" };
string output = string.Join(",", items);

produces: Red,Green,,Blue

2 separators.
Depends on what output is required for a null entry.

// Looks like my last sent message errored out on the server.

I got it.
Yes, whether it works, depends on your definition of works. :)

What I meant was, it produces a string, that when run through
string.split(), faithfully reproduces the input list of strings.

And if the empty element is not wanted then there is the
option of:

string output = items.Aggregate((tot,nxt) => tot + (nxt != null ? "," +
nxt : ""));

Arne
 
Z

Zach

Steve Thackery wrote:
The OP might benefit from some practice simply reviewing the
documentation for the classes he is working with.

Pete

Like a painter, who does not have all the colors he is able to buy at
the art shop on his pallet, any practitioner uses only a subset of what
is available. What the artist has on his pallet is determined by the
things he usually paints. But indeed, and maybe that is your message,
one should revise from time to time, walk around the paint shop
occasionally. The trouble is that books on computer languages are often
such disasters in terms of their communicative efficacy given that every
word is a dollar and that many authors have never stood before a bunch
of semi-bright students, or stood before a bunch of critical executives
wondering how much more of their time is going to be wasted by the speaker.

Zach.
 
A

Arne Vajhøj

Like a painter, who does not have all the colors he is able to buy at
the art shop on his pallet, any practitioner uses only a subset of what
is available. What the artist has on his pallet is determined by the
things he usually paints. But indeed, and maybe that is your message,
one should revise from time to time, walk around the paint shop
occasionally. The trouble is that books on computer languages are often
such disasters in terms of their communicative efficacy given that every
word is a dollar and that many authors have never stood before a bunch
of semi-bright students, or stood before a bunch of critical executives
wondering how much more of their time is going to be wasted by the speaker.

Hm.

The .NET framework docs are freely available at the internet
and for download and install at ones own PC.

I take longer time to post a question to the internet than
to skim all the methods for a given class.

It makes sense from every perspective to read the docs.

Arne
 

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