Understanding the SystemVerilog String Data Type
The string
data type is an important data type in SystemVerilog that is used to represent text data. A string literal is a packed array of multiple of 8 bits that can be converted to a string
data type. When a string literal is assigned to a packed array of an integral variable of a different size, it is either truncated to the size of the variable or padded with zeros to the left as necessary. However, when using the string
data type, strings can be of arbitrary length and no truncation occurs.
In addition, a string
variable shall not contain the null character '\0'
. Assigning the value 0 to a string character shall be ignored.
Operators
The following operators are used with the string
data type:
Operator | Description |
---|---|
== | Equal. Each side can be a string data type or string literal. When compared, they are implicitly converted to a string data type. |
!= | Not equal. |
< | Less than. Comparison is based on lexicographic order. |
<= | Less than or equal. Comparison is based on lexicographic order. |
> | Greater than. Comparison is based on lexicographic order. |
>= | Greater than or equal. Comparison is based on lexicographic order. |
{str1, str2} | Concatenation. Concatenates two or more strings together. |
{multiplier{str}} | Replication. Repeats a string a given number of times. |
str[index] | Indexing. Returns a byte, the ASCII code at the given index. |
The {str1, str2}
operator can be used to concatenate two or more strings together. The {multiplier{str}}
operator can be used to repeat a string a given number of times.
The []
operator can be used to access individual characters within a string. The index
is zero-based and the operator returns a byte, which represents the ASCII code of the character at the given index.
String Methods
The string
data type has several built-in methods for manipulating strings:
len()
The len()
method returns the length of the string.
string myString = "Hello World";
$display("The length of the string is %0d", myString.len());
// Output: The length of the string is 11
putc(int i, byte c)
The putc()
method replaces the ith character with the given value.
string myString = "Hello World";
myString.putc(0, 'h');
$display("The new string is %s", myString);
// Output: The new string is hello World
getc(int i)
The getc()
method returns the ASCII code of the ith character.
string myString = "Hello World";
$display("The ASCII code of the first character is %0d", myString.getc(0));
// Output: The ASCII code of the first character is 72
toupper()
The toupper()
method returns a new string
data type with all characters in uppercase.
string myString = "hello world";
string myNewString = myString.toupper();
$display("The new string is %s", myNewString);
// Output: The new string is HELLO WORLD
tolower()
The tolower()
method returns a new string
data type with all characters in lowercase.
string myString = "HELLO WORLD";
string myNewString = myString.tolower();
$display("The new string is %s", myNewString);
// Output: The new string is hello world
compare(string s)
The compare()
method returns 0 if the strings are equal, -1 if the first string is lexicographically less than the second string, and 1 if the first string is lexicographically greater than the second string.
string myString1 = "hello";
string myString2 = "world";
$display("The result of the comparison is %0d", myString1.compare(myString2));
// Output: The result of the comparison is -1
icompare(string s)
The icompare()
method is similar to compare()
, but performs a case-insensitive comparison.
string myString1 = "Hello";
string myString2 = "hello";
$display("The result of the comparison is %0d", myString1.icompare(myString2));
// Output: The result of the comparison is 0
substr(int i, int j)
The substr()
method returns a new string
data type formed by characters in position i through j of the original string.
string myString = "Hello World";
string mySubstring = myString.substr(6, 10);
$display("The substring is %s", mySubstring);
// Output: The substring is World
atoi()
, atohex()
, atooct()
, atobin()
These methods convert a string to an integer in the corresponding base.
string myString = "1234";
int myInt = myString.atoi();
$display("The integer value is %0d", myInt);
// Output: The integer value is 1234
atoreal()
The atoreal()
method converts a string to a real number.
string myString = "3.14";
real myReal = myString.atoreal();
$display("The real value is %0f", myReal);
// Output: The real value is 3.140000
itoa(integer i)
, hextoa(integer i)
, octtoa(integer i)
, bintoa(integer i)
, realtoa(real r)
These methods convert a number to a string.
int myInt = 1234;
string myString = "";
myString.itoa(myInt);
$display("The string value is %s", myString);
// Output: The string value is 1234
Conclusion
The string
data type is an important data type in SystemVerilog that is used to represent text data. By using the string
data type, you can store and manipulate text data in a SystemVerilog design. You can use operators to compare and concatenate strings, and built-in methods to manipulate and convert strings.