String

String

In SystemVerilog, strings are dynamic arrays of characters, used to hold textual data. They can automatically resize to hold any number of characters. The string is an integral part of SystemVerilog and is crucial for handling and manipulating text.

module MyModule;
    string greetings= "Hello, World!";
    initial begin
        $display(greetings);
    end
endmodule

In this example, the string greetings holds the text "Hello, World!" and then displays this text when the module initializes.

In Verilog, strings aren't natively supported as a specific data type. If you wanted to represent a string in classical Verilog, you'd have to use a workaround, such as using an array of characters. However, in SystemVerilog, an extension of Verilog, strings are a first-class datatype, and you can handle them much more intuitively.

But let's talk about Verilog a bit since you may encounter Verilog RTLs where strings are used with some workaround

Verilog "Strings": In classic Verilog, if you needed to represent a string, you would use a workaround:

Character Array Workaround:

reg [8*N:1] greetings="Hello, World!"; // Where N is the number of characters, in this case 13.

String Methods

In SystemVerilog, the string data type comes with several built-in methods to manipulate and analyze string data. These methods make handling strings more convenient. Here are some of the commonly used string methods:

  1. len():

    • Returns the length of a string.

      string str = "hello";
      $display(str.len());  // Outputs: 5
  2. putc(int index, byte ch):

    • Replaces the character at the specified index with the character ch.

      string str = "hello";
      str.putc(0, "H");
      $display(str);  // Outputs: Hello
  3. getc(int index):

    • Returns the character at the specified index.

      string str = "hello";
      byte ch;
      ch = str.getc(1);
      $display(ch);  // Outputs: e
  4. tolower():

    • Converts all characters in a string to lowercase.

      string str = "HELLO";
      $display(str.tolower());  // Outputs: hello
  5. toupper():

    • Converts all characters in a string to uppercase.

      string str = "hello";
      $display(str.toupper());  // Outputs: HELLO
  6. compare(string with):

    • Compares two strings. It returns 0 if the strings are the same, a negative value if the calling string is less than the string with, or a positive value if the calling string is greater.

      string str1 = "hello";
      string str2 = "world";
      if (str1.compare(str2) < 0) $display("str1 comes before str2");
  7. icompare(string with):

    • Similar to compare, but is case-insensitive.

      string str1 = "HELLO";
      string str2 = "hello";
      if (str1.icompare(str2) == 0) $display("str1 is the same as str2 (ignoring case)");
  8. substr(int start, int finish):

    • Extracts a substring from the string starting from start index and ending at finish index.

      string str = "SystemVerilog";
      $display(str.substr(6, 10));  // Outputs: Veri

There are more string methods available, but these are some of the most frequently used. With these methods, manipulating and analyzing strings becomes easier and more efficient in SystemVerilog.

Have a Question?

Feel free to ask your question in the comments below.

Please Login to ask a question.

Login Now