The string data-type is an ordered collection of characters. The length of a string variable is the number of characters in the collection which can have dynamic length and vary during the course of a simulation. A string variable does not represent a string in the same way as a string literal. No truncation occurs when using the string variable.
Syntax: string variable_name [= initial_value];
variable_name is a valid identifier and the optional initial_value can be a string literal, the value "" for an empty string, or a string data type expression. If an initial value is not specified at the time of declaration, then the variable defaults to "", an empty string literal.
A single ASCII character requires 8-bits (1 byte) and to store a string we would need as many bytes as there are number of characters in the string.
Example:
module string_data_type;
reg [0:15*8-1]s;
string s0;
bit[31:0]b;
initial begin
s = "how are you doing";//17chars
$display("String s = %0s",s);
s = "Hello World";//11chars
$display("String s = %0s",s);
//s0-> empty string
$display("String s0 = %0s",s0);
s0 = "Hello World ssssss";//11chars
$display("String s0 = %0s",s0);
s0 = string'({"Hi, "," ",s});
$display("String s0 = %0s",s0);
// Bug
b = 128;
s0 = string'(b); // sets 128 to s0
$display("s0 = %0s \n bit b = %0d",s0,b);
$sformat(s0, "%s", b);
$display("s0 = %0s \n bit b = %0d",s0,b);
s0="welcome to We_LSI channel,practice at the end of the session \
All the best";
$display("s0 = %0s",s0);
end
endmodule
Example:
//-------------------------------------------------------
module string_operator;
string s1="Hi Everyone";//11
string s2="He Everyone";//11
string s3;
initial begin
if(s1==s2)
$display("s1=%0s equals to s2=%0s",s1,s2);
else $display("s1 is not equals to s2");
if(s1<s2)//<=,>,>=
$display("s1=%0s is less than s2=%0s",s1,s2);
else
$display("s1 is not less than s2");
s3={s1," welcome ",s2};
$display("s3=%0s",s3);
s3={3{"We_LSI"}}; //{5{s1}}
$display("s3=%0s",s3);
s3="Hello guys";
$display("s3=%0d s3=%0s",s3[7],s3[7]);
end
endmodule
Example:
//-------------------------basic methods---------------------------
module string_basic_methods;
string s1="Hi Everyone";//11
string s2="He Everyone";//11
string s3;
initial begin
$display("s1=%0d",s1.len());
s1.putc(2,"char");
$display("s1=%0s",s1);
// s1.putc(2,"char") //s1=Hiceveryone
$display("s1=%0s=%0d", s1.getc(2), s1.getc(2));
s1="ENGINEERING";
$display("s1=%0s",s1.tolower());
$display("s1=%0s",s1.toupper());
// s2="ENGINEERING";
s2="EZgineering";//A=65 a=97
$display("s1=%0s s2=%0s difference=%0d",s1,s2,s1.compare(s2));
//(s1-s2)case sensitive(0=equ,neg=s1<s2,pos=s1>s2)
$display("s1=%0s s2=%0s difference=%0d",s1,s2,s1.icompare(s2)); //case insensitive
end
endmodule
https://www.chipverify.com/systemverilog/systemverilog-strings
Book System Verilog for Verification by CHRIS SPEAR