String Data type in System Verilog
We have seen in Verilog that there is no data type to store strings. Instead, reg data type was used to store strings. In System Verilog, the string data type can be used to store strings. String also provides various methods which are helpful for string operations like finding the length of string, etc.
Declaration
The syntax for the string data type is the same as that of other types. By default, the value of a string type is “”
, i.e., a blank character. If no content needs to be stored in the string data type, then “”
can be used.
Syntax
string str; // default value will be ""
string str = "Hello World!!";
The string is a particular data type whose size changes dynamically during the run time. The size is automatically determined by the simulator based on the number of characters on the string. In Verilog, this is a big problem as we must manually calculate the size of the reg data type.
Basic operations on string
Like other data types, all the operators work with string data types also.
String Comparisons
String comparison canbe done with the help of operators or also with the help of inbuilt string functions.
str1 == str2
, compares 2 string and returns 1 if both are samestr1 != str2
, returns 1 if both the string are not same.str1 > str2
str1 >= str2
str1 < str2
str1 <= str2
For string comparisons, the ASCII equivalent of the characters is used. Thus, all the comparisons are case sensitive when we use operators for comparison. ASCII codes of upper and lower case characters are different. For case-insensitive comparison, a built in method of string type can be used.
Concatenation Operator
{str1, str2, …, strN}
– Concatenates all the strings into one string.
Repetition Operator
{M{str1}}
– repeats the string, M
times and returns the result. This operator is very helpful when we want to make an
Indexing Operator
str[index]
– returns the character present at the index location. As with the case of arrays in a string, the index starts from 0 and ends at index-1.
String Methods
String methods are system functions that specifically work with the string data type. These methods can be used to perform various operations on a string.
len()
This function returns the number of characters present in a string. Even a space “ “ will be treated as a character and included in the number of characters.
Syntax – str.len();
putc(int i, byte c)
This function inserts a character at a given position. There are two inputs to this function, position and character.
Syntax – str.putc(2, “a”);
getc(int i)
This function returns the character present at index i. This function has only 1 argument, i.e., the index i.
Syntax – bit [7:0] ch = str.getc(i);
substring()
This function returns a part of the string according to the index specified in the parameters. First parameter is the starting index from which the substring should start and the second parameter is the last index which will be returned.
Syntax – sub_str = str.substring(i, j);
toupper()
This function converts a string into an upper-case string. Thus, the returned string will have all characters in upper case.
tolower()
This function converts a string into a lower-case string. Thus, the returned string will have all characters in lower case.
Example
module str_manip ();
string a = "AEIOU";
string b = "alonso";
string d = "Alonso";
string c = "Hello";
byte char;
string subs2, lc, uc;
string rep_c;
integer size, val;
initial begin
// String methods
size = a.len();
char = b.getc(5);
uc = b.toupper();
lc = a.tolower();
subs2 = a.substr(1,3);
$display("Size of sring a is %d", size);
$display("5th character of string b is %s", char);
$display("string b in upper case %s", uc);
$display("String a in lower case %s", lc);
$display("Substring from string a is %s\n", subs2);
// Repetition operator
rep_c = {4{c}};
$display("Repetition operator: {4{%s}} = %s", c, rep_c);
// Concatenation operator
$display("Concatenation operator: {%s, %s} = %s\n", c, b, {c, b} );
// String comparision
if(b == d)
$display("%s and %s are same", b, d);
else begin
$display("%s and %s are not same", b, d);
end
if(b == b)
$display("%s and %s are same", b, b);
else begin
$display("%s and %s are not same", b, b);
end
if(b >= d)
$display("%s is greater than %s", b, d);
else begin
$display("%s is samller than %s", b, d);
end
end
endmodule
Output
# Size of sring a is 5
# 5th character of string b is o
# string b in upper case ALONSO
# String a in lower case aeiou
# Substring from string a is EIO
#
# Repetition operator: {4{Hello}} = HelloHelloHelloHello
# Concatenation operator: {Hello, alonso} = Helloalonso
#
# alonso and Alonso are not same
# alonso and alonso are same
# alonso is greater than Alonso
Try this code in EDA Playground