Ready-to-run snippets

Code Examples

Ready-to-run Verilog, SystemVerilog, and UVM examples. Click any to open it live in the IDE.

← All examples

Strings Example

SystemVerilog Data Types

Runnable example from the Systemverilog Strings tutorial.

Open in IDE
testbench.sv
module tb;
  string firstname = "Joey";
  string lastname  = "Tribbiani";
  
  initial begin
    // String Equality : Check if firstname equals or not equals lastname
    if (firstname == lastname)
      $display ("firstname=%s is EQUAL to lastname=%s", firstname, lastname);
    
    if (firstname != lastname)
      $display ("firstname=%s is NOT EQUAL to lastname=%s", firstname, lastname);
    
    // String comparison : Check if length of firstname < or > length of lastname
    if (firstname < lastname)
      $display ("firstname=%s is LESS THAN lastname=%s", firstname, lastname);
    
    if (firstname > lastname)
      $display ("firstname=%s is GREATER THAN lastname=%s", firstname, lastname);
    
    // String concatenation : Join first and last names into a single string
    $display ("Full Name = %s", {firstname, " ", lastname});
    
    // String Replication
    $display ("%s", {3{firstname}});
    
    // String Indexing : Get the ASCII character at index number 2 of both first and last names
    $display ("firstname[2]=%s lastname[2]=%s", firstname[2], lastname[2]);
    
  end
endmodule
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.