SystemVerilog: how to extract parts of a string - verilog

This is a discussion on SystemVerilog: how to extract parts of a string - verilog ; If I have a string: string s = "top.block_name.subblock_name"; and I want to extract the block_name from the string, how would I do it? I know there's a substr function s.substr() but there's no find() function to locate the . ...

+ Reply to Thread
Results 1 to 2 of 2

SystemVerilog: how to extract parts of a string

  1. Default SystemVerilog: how to extract parts of a string

    If I have a string:

    string s = "top.block_name.subblock_name";

    and I want to extract the block_name from the string, how would I do
    it?

    I know there's a substr function

    s.substr()

    but there's no find() function to locate the . (dot) separator.

    Thanks!

  2. Default Re: SystemVerilog: how to extract parts of a string

    On Wed, 16 Apr 2008 15:23:28 -0700 (PDT), philip.chen@gmail.com wrote:

    >If I have a string:
    >
    >string s = "top.block_name.subblock_name";
    >
    >and I want to extract the block_name from the string, how would I do
    >it?
    >
    >I know there's a substr function
    >
    >s.substr()
    >
    >but there's no find() function to locate the . (dot) separator.


    You can always write your own find function:

    function int find(int offset, string s);
    int i;
    for (i = offset; i < s.len(); i=i+1)
    if (s.getc(i) == '.')
    find = i;
    return;
    endfunction

    and use it as following:

    int firstdot = find(0,s)+1;
    int nextdot = find(firstdot,s);
    string blockname = s.substr(firstdot, nextdot);

    Standard disclaimer: this is non-tested code which means most probably
    it won't even compile at first try. Making it work is left as an
    exercise for the reader.

+ Reply to Thread