Thread overview
std.string.split
Apr 14, 2004
Scott Egan
Apr 14, 2004
Ant
Apr 14, 2004
Scott Egan
Apr 15, 2004
Ant
Re: std.string.split - a solution
Apr 15, 2004
Scott Egan
April 14, 2004
May I suggest that a third split function be offered.  In VB(.net) there is the option to split and limit the number of returned parts, eg:

dim str as string = "QAZ = Test Log value = 5"
dim parts as string()
parts = str.split("=", 2)

This results in parts(0) = "QAZ " and parts(1) = " Test Log value = 5"

I find this incredibly useful in log file and other text manipulation work.

So the D equivalent should be:
char[][] split(char[] s, char[] delim, int parts)

Thoughts?


April 14, 2004
In article <c5jf7p$1hfv$1@digitaldaemon.com>, Scott Egan says...
>
>May I suggest that a third split function be offered.  In VB(.net) there is the option to split and limit the number of returned parts, eg:
>
>dim str as string = "QAZ = Test Log value = 5"
>dim parts as string()
>parts = str.split("=", 2)
>
>This results in parts(0) = "QAZ " and parts(1) = " Test Log value = 5"
>
>I find this incredibly useful in log file and other text manipulation work.
>
>So the D equivalent should be:
>char[][] split(char[] s, char[] delim, int parts)
>
>Thoughts?

It's usefull.
I needed it.
I use it (just rejoin after split).
let's got for it.

Ant


April 14, 2004
Good so how do I help?

"Ant" <Ant_member@pathlink.com> wrote in message news:c5k2g6$2ea0$1@digitaldaemon.com...
> In article <c5jf7p$1hfv$1@digitaldaemon.com>, Scott Egan says...
> >
> >May I suggest that a third split function be offered.  In VB(.net) there
is
> >the option to split and limit the number of returned parts, eg:
> >
> >dim str as string = "QAZ = Test Log value = 5"
> >dim parts as string()
> >parts = str.split("=", 2)
> >
> >This results in parts(0) = "QAZ " and parts(1) = " Test Log value = 5"
> >
> >I find this incredibly useful in log file and other text manipulation
work.
> >
> >So the D equivalent should be:
> >char[][] split(char[] s, char[] delim, int parts)
> >
> >Thoughts?
>
> It's usefull.
> I needed it.
> I use it (just rejoin after split).
> let's got for it.
>
> Ant
>
>


April 15, 2004
On Thu, 15 Apr 2004 08:10:22 +1000, Scott Egan wrote:

> 
> Good so how do I help?
> 

I don't know.
meanwhile you can use this crude implementation:

char[][] split (char[] string, char[] delim, int parts)
{
	char[][] tks = std.string.split(string, delim);
	if ( parts>0 && tks.length>parts )
	{
		tks[parts-1] = std.string.join(tks[parts-1..tks.length],delim);
		tks.length = parts;
	}
	return tks;
}

Ant

April 15, 2004
import std.c.stdio;
import std.string;

/**************************************
 * Split s[] into an array of words,
 * using delim[] as the delimiter.
 * Stop when the number of parts have
 * been found.
 */

char[][] split(char[] s, char[] delim, int parts)
in {
 assert(delim.length > 0);
 assert(parts >= 0);
}
body {
 uint count = 0;
 uint last = 0;
 uint current;
 char[][] words;

 // If the deliminator won't fit in the string then just return the string.
 if(s.length < delim.length) {
  words = new char[][1];
  words[0] = s;
  return words;
 }

 // init the returned array - lucky we know max size;
 words = new char[][parts];  // is words.length = parts justas good?

 // chars to check and parts to get
 for(current = 0; (current <= s.length - delim.length) && (count < parts -
1); current++)
 {
  if(s[current..current + delim.length] == delim)
  {
   // printf("%i, %i, %i \n", last, current, count);
   words[count] = s[last..current];  // .dup ????
   current += delim.length; // step over deliminator
   last = current;
   count++;
  }
 }
 words[count] = s[last..s.length];  // remainder of string
 words.length = count + 1;   // trim the returned arry to correct length -
if all ready correct does this do anything ???
 return words;
}