www.digitalmars.com         C & C++   DMDScript  

D - std.string.split

reply "Scott Egan" <scotte tpg.com.aux> writes:
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?
Apr 14 2004
parent reply Ant <Ant_member pathlink.com> writes:
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
Apr 14 2004
parent reply "Scott Egan" <scotte tpg.com.aux> writes:
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


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


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

Apr 14 2004
parent reply Ant <duitoolkit yahoo.ca> writes:
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
Apr 14 2004
parent "Scott Egan" <scotte tpg.com.aux> writes:
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;
}
Apr 15 2004