String Split in SV

In our verification environment, we may need to do some kind of string manipulations. In scripting languages like perl, this is done by using just a method (split). In SV, we need to do it in a roundabout way by parsing all the characters by using getc method.

Below example shows how to split a string using ” : ” as delimiter.

module split_string_module;

string str_vlsi;

int p_offset_in = 0;
int p_offset_out = 0;
int count = 0;`

string str[$];

function string split_using_delimiter_fn(input int offset, string str,string del,output int cnt);
  for (int i = offset; i < str.len(); i=i+1) 
    if (str.getc(i) == del) begin
       cnt = i;
       return str.substr(offset,i-1);

initial begin
   #10; str_vlsi = "";
   #10; $display($time,"   STRING = %s",str_vlsi);
   for(int j=0; j<4; j = j+1) begin
       str[j] = split_using_delimiter_fn(p_offset_in,str_vlsi,":",p_offset_out);
       $display($time,"   offset_in=%0d offset_out=%0d",p_offset_in,p_offset_out);
       if(p_offset_in >= p_offset_out) 
          str[j] = str_vlsi.substr(p_offset_in,str_vlsi.len()-1);
          p_offset_in = p_offset_out+1;

       $display($time,"   Splitted String %d = %s",j,str[j]);

Simulation result:

                  20   STRING =
                  20   offset_in=0 offset_out=4
                  20   Splitted String           0 = vlsi
                  30   offset_in=5 offset_out=18
                  30   Splitted String           1 = professionals
                  40   offset_in=19 offset_out=21
                  40   Splitted String           2 = in
                  50   offset_in=22 offset_out=21
                  50   Splitted String           3 =

The function split_using_delimiter_fn, parses through each character in the string till it finds the delimiter.

2 comments on “String Split in SV

Leave a Reply

Your email address will not be published. Required fields are marked *