Is this still just me : c# string split and join
-
Just written some code which needed converting an array to a string and back, and was like WTF. to split a string into an array
var joined_str = String.Join(",", my_array);
cool. To split a string into array:
var my_array = joined_str.Split(',');
Split only really wants a char, and Join only wants a string. Why can't this two things not get along. Maybe just me and some outof date c#.net :(
maze3 wrote:
Split only really wants a char, and Join only wants a string.
Usually splitting a string by just a char makes sense, however you should look at the method signature, it's actually not a
char
:public String[] Split(params char[] separator);
(One of several array overloads) Joining by a string makes tons of sense. For example, joining by CRLF.
Latest Article - Azure Function - Compute Pi Stress Test Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802
-
Just written some code which needed converting an array to a string and back, and was like WTF. to split a string into an array
var joined_str = String.Join(",", my_array);
cool. To split a string into array:
var my_array = joined_str.Split(',');
Split only really wants a char, and Join only wants a string. Why can't this two things not get along. Maybe just me and some outof date c#.net :(
maze3 wrote:
var joined_str
maze3 wrote:
var my_array
You don't mention why those two don't work well together, but could that overused three letter keyword be what obscures the actual problem?
I have lived with several Zen masters - all of them were cats. His last invention was an evil Lasagna. It didn't kill anyone, and it actually tasted pretty good.
-
Just written some code which needed converting an array to a string and back, and was like WTF. to split a string into an array
var joined_str = String.Join(",", my_array);
cool. To split a string into array:
var my_array = joined_str.Split(',');
Split only really wants a char, and Join only wants a string. Why can't this two things not get along. Maybe just me and some outof date c#.net :(
BTW, I have a persistent string parser article I wrote in 2008. It might give to a starting point for rolling your own code. Keep in mind that I wrote this when I was just starting out in .Net, so refactoring it might be a good call. Persistent String Parser[^]
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013 -
Just written some code which needed converting an array to a string and back, and was like WTF. to split a string into an array
var joined_str = String.Join(",", my_array);
cool. To split a string into array:
var my_array = joined_str.Split(',');
Split only really wants a char, and Join only wants a string. Why can't this two things not get along. Maybe just me and some outof date c#.net :(
-
I am struggling to understand why you need to convert array to string, and then back to array?
To be sure, to be sure... ;)
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
I am struggling to understand why you need to convert array to string, and then back to array?
-
I am struggling to understand why you need to convert array to string, and then back to array?
musefan wrote:
I am struggling to understand why you need to convert array to string, and then back to array?
It's talking to a COBOL service. ;P
Latest Article - Azure Function - Compute Pi Stress Test Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802
-
You can write an extension method for
split
that callsjoin
to hide the nastiness, and always callsplit
from then on.".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013I was just about to write the exact same message. :thumbsup:
cheers Chris Maunder
-
Just written some code which needed converting an array to a string and back, and was like WTF. to split a string into an array
var joined_str = String.Join(",", my_array);
cool. To split a string into array:
var my_array = joined_str.Split(',');
Split only really wants a char, and Join only wants a string. Why can't this two things not get along. Maybe just me and some outof date c#.net :(
maze3 wrote:
Split only really wants a char,
Not sure what you mean by "only really". :) It also accepts an array of
char
separators. But I wish it also had an overload that accepted a string that was interpreted as a set ofchar
s. It's much easier to type. /raviMy new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
Just written some code which needed converting an array to a string and back, and was like WTF. to split a string into an array
var joined_str = String.Join(",", my_array);
cool. To split a string into array:
var my_array = joined_str.Split(',');
Split only really wants a char, and Join only wants a string. Why can't this two things not get along. Maybe just me and some outof date c#.net :(
Am I missing something? There's an overload, albeit an ugly one:
string s = "Rob and Bob and Clob";
var z = s.Split(new[] {" and "}, StringSplitOptions.None);Regards, Rob Philpott.
-
You can write an extension method for
split
that callsjoin
to hide the nastiness, and always callsplit
from then on.".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013I had never used
Join
before, and after looking it up, it doesn't do the same thing as split, so my original response is crap. Join concatenates strings together, while split breaks a string up based on a delimiter character.".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013