String Type

A string in SystemVerilog is an ordered collection of characters. The length of a string may vary during simulation. If a string literal is assigned to a string variable during simulation, the size of the variable is adjusted so that neither the literal is truncated nor the variable is padded with zeros.

string declaration

string str;

string Intilization

string str = "initial value";

size of the variable will be adjusted automatically.

You can use square brackets to select one character from the string. Square brackets can be used both on the left and on the right side of the assignment, for example: string str = "initial value";
str1[1] = str2[0];

A string variable can be used in the $display statement, for example:

$display ("str = %s", str);

string methods

SystemVerilog also includes a number of special methods to work with strings. These methods use the built-in method notation.
Method Example Description
.len() function int len(), str.len() returns the length of the string, i.e., the number of characters in the string
.putc() function void putc (int i, byte c), str.putc(i, c) replaces the i-th character in str with the given integral value,
.getc() function byte getc(int i) str.getc(i) returns the ASCII code of the ith character in str.
If i < 0 or i >= str.len(), then str.getc(i) returns 0.
The getc method assignment x = str.getc(j) is semantically equivalent to x = str[j].
.getc() function byte getc(int i) str.getc(i) returns the ASCII code of the ith character in str.
If i < 0 or i >= str.len(), then str.getc(i) returns 0.
The getc method assignment x = str.getc(j) is semantically equivalent to x = str[j].
Toupper() function string toupper(); str.toupper() returns a string with characters in str converted to uppercase. str is unchanged.
Tolower() function string tolower(); str.tolower() returns a string with characters in str converted to lowercase. str is unchanged.
Compare() function int compare(string s); tr.compare(s) compares str and s, as in the ANSI C strcmp function with regard to lexical ordering and return value.
Icompare() function int icompare(string s); str.icompare(s) compares str and s, like the ANSI C strcmp function with regard to lexical ordering and return value, but the comparison is case insensitive.
Substr() function string substr(int i, int j); str.substr(i, j) returns a new string that is a substring formed by characters in position i through j of str. If i < 0, j < i, or j >= str.len(), substr() returns " " (the empty string).
Atoi(),
atohex(),
atooct(),
atobin()
function integer atoi();
function integer atohex();
function integer atooct();
function integer atobin();
str.atoi() returns the integer corresponding to the ASCII decimal representation in str(string).
Atoreal() function real atoreal(); str.atoreal() returns the real number corresponding to the ASCII decimal representation in str. The conversion parses for real constants. The scan stops as soon as it encounters any character that does not conform to this syntax or the end of the string. It returns zero if no digits were encountered.
Itoa() function void itoa(integer i); sstr.itoa(i) stores the ASCII decimal representation of i into str (inverse of atoi).
Hextoa() function void hextoa(integer i); str.hextoa(i) stores the ASCII hexadecimal representation of i into str (inverse of atohex).
Octtoa() function void octtoa(integer i); str.octtoa(i) stores the ASCII octal representation of i into str (inverse of atooct).
Bintoa() function void bintoa(integer i); str.bintoa(i) stores the ASCII binary representation of i into str (inverse of atobin).
Realtoa() function void realtoa(real r); str.realtoa(r) stores the ASCII real representation of r into str (inverse of atoreal).




String operators:

SystemVerilog provides a set of operators that can be used to manipulate combinations of string variables and string literals.
The basic operators defined on the string data type are The following operators can be used for comparing strings: ==, !=, <, >, <=, and >=. Comparisons are case sensitive.

Equality

The String operators are used in conditional statements and looping statements:
Str1 == Str2

Checks whether the two string operands are equal. Result is 1 if they are equal and 0 if they are not. Both operands can be expressions of string type, or one can be an expression of string type and the other can be a string literal, which shall be implicitly converted to string type for the comparison. If both operands are string literals, the operator is the same equality operator as for integral types.

Inequality

Str1 != Str2

Checks whether the two string operands are equal. Result is 1 if they are equal and 0 if they are not. Both operands can be expressions of string type, or one can be an expression of string type and the other can be a string literal, which shall be implicitly converted to string type for the comparison. If both operands are string literals, the operator is the same equality operator as for integral types.

Equality

Str1 == Str2

Checks whether the two string operands are equal. Result is 1 if they are equal and 0 if they are not. Both operands can be expressions of string type, or one can be an expression of string type and the other can be a string literal, which shall be implicitly converted to string type for the comparison. If both operands are string literals, the operator is the same equality operator as for integral types.

Comparison:

Str1 < Str2
Str1 <= Str2
Str1> Str2
Str1 >= Str2

Relational operators return 1 if the corresponding condition is true using the lexicographic ordering of the two strings Str1 and Str2. The comparison uses the compare string method. Both operands can be expressions of string type, or one can be an expression of string type and the other can be a string literal, which shall be implicitly converted to string type for the comparison. If both operands are string literals, the operator is the same comparison operator as for integral types.

Concatenation

{Str1,Str2,...,Strn}

Each operand can be a string literal or an expression of string type. If all the operands are string literals the expression shall behave as a concatenation of integral values; if the result of such a concatenation is used in an expression involving string types then it shall be implicitly converted to string type. If at least one operand is an expression of string type, then any operands that are string literals shall be converted to string type before the concatenation is performed, and the result of the concatenation shall be of string type.

Replication:

{Str1,Str2,...,Strn}

Str can be a string literal or an expression of string type. multiplier shall be an expression of integral type, and is not required to be a constant expression. If multiplier is non-constant or Str is an expression of string type, the result is a string containing N concatenated copies of Str, where N is specified by the multiplier. If Str is a literal and the multiplier is constant, the expression behaves like numeric replication.

Indexing

Str[index]

Returns a byte, the ASCII code at the given index. Indices range from0 to N–1, where N is the number of characters in the string. If given an index outof range, returns 0.

String Exapmples

palindrome

program main;
string str = " kayak ";
string str1 = str;
initial
begin
foreach(str[i])
str1[str.len-1-i] = str[i];
$display("str1: %s",str1);
if (str == str1)
$display("string is palindromen");
else
$display("string is not palindromen");
end
endprogram

OUT PUT :

# str1: kayak
# string is palindromen

reverse_string

// reverse string using for loop program main;
string str = "VLSI";
string str1 = str;

initial
begin
$display("String length: %0d",str.len);
for (int i=0; i < str.len(); i++)
str1.putc((str.len-1-i) ,str.getc(i));
$display("str1: %s",str1);

OUT PUT :

# String length: 13
# str1: ISLV

reverse_string_using_array

program main;
string str = "Hello World!";
string str1 = str;

initial begin
foreach(str[i])
str1[str.len-1-i] = str[i];
$display("str1: %s",str1);
end
endprogram

OUT PUT :

# str1: LAIROTUT ISLV

String Exapmples

module string_tb();
string sentence = "This is a orginal string";
string statement;

initial begin
$display ("Sentence = %s",sentence);

// Assign new string of different size
sentence = "This is a new string of different length";
$display ("Sentence = %s",sentence);


// Change to uppercase and assign to new string
statement = sentence.toupper();
$display ("Statement = %s",statement);


// Get the length of string
$display("Length of statement string: %d", statement.len());


if(statement.tolower() == sentence.toupper())
begin
$display("Lower case and upper case match (unusual!)");
end

if(statement.tolower() == sentence.tolower())
begin
$display("Case insensitive comparison matches");
end

#1 $finish;
end

endmodule

String Exapmples

// declare a string...
// exame string's methods:
// len()
// toupper/tolower
module string_ex ();

string my_string = "This is a string";
string my_new_string;

initial begin
$display ("My String = %s",my_string);

// the first char
$display ("First char of My String = %s",my_string[0]);
// the last char

$display ("Last char of My String = %s",my_string[my_string.len()-1]);


// runtime error: part-select reversed
//$display ("Part of My String = %s",my_string[0:3]);


// '{84, 104, 105, 115}
$display ("Part of My String = %p",my_string[0:3]);


// Assign new string of different size
my_string = "This is a new string of different length";
$display ("My String = %s",my_string);


// Change to uppercase and assign to new string
my_new_string = my_string.toupper();
$display ("My New String = %s",my_new_string);


// Get the length of sting
$display ("Length of new string %0d",my_new_string.len());


// Compare variable to another variable
if (my_string.tolower() == my_new_string.tolower()) begin
$display("String Compare matches");
end


// Compare variable to variable
if (my_string.toupper() == my_new_string) begin
$display("String Variable Compare matches");
end

#1 $finish;
end
endmodule

String Exapmples

program main;
int i,k,p;
string str = "aBCd"; // declare string variables
string new_str;
initial begin
new_str = str.toupper(); // assigns ABCD to new_str; str is not changed
$display ("new_str = %s", new_str);
// new_str = str.tolower(); // assigns abc to new_str; str is not changed
str.putc(0, "e"); ; // changes str to eBCd
$display ("str = %s", str);
str.putc(5, "e"); // has no effect, str is not expanded eBCd
$display ("str = %s", str);

i = str.len(); // returns the length of str (=4)
$display ("No of char in str i = %d", i);
p = str.getc(3); // gets the character at position 3 (=3)
$display ("the 3rd position of str is p = %s", p);
k = str.icompare(new_str); // compares str to new_str; the
if(k)

$display (" the string str and new_str are match ");
else
$display (" the string str and new_str are not match ");

#1 $finsh;
end

endprogram

OUT PUT :

# new_str = ABCD
# str = eBCd
# str = eBCd
# No of char in str i = 4
# the 3rd position of str is p = d
# the string str and new_str are match