String data type :
System Verilog provides “string” data type for storing characters . In System Verilog the string data type is of variable size. The size will be dynamically allocated during runtime as array of bytes. Each character will occupy 1-byte (8 bits) .
–> Variables of type string can be indexed from “0 to n-1(the last element of array)” .
–> If the initial values are not specified during declaration , the variable will be initialized to ” ” i.e empty string.
Syntax : data_type variable_name;
Example : string emp_name = “john”; // emp_name is variable and intialized to string john.
string subj_name =” “; // initialized as empty string
–> The characters (strings) can be assigned to either “string” data type or “integer” data type also . For assigning to a string data type there is no need of mentioning size as size is variable. But when assigning to a integer type variable size is needed.
Example-1: string student_name = ” John” ;
here in example-1 we declared a variable “student_name” as string data type . As string data type is varibale size we did’t mention any size.
Example-2: bit [3:0][7:0]student_name;
student_name = “John”
In the example-2 we declared a varible “student_name” as bit data type along with size . bit data type is not varibale size , so we declared specific size required for storing characters. we are storing 4 characters([3:0]) i.e”John ” , each character will occupy 8 bits([7:0]) of data and hence we declared total 32 bits as packed array.
Operators for string data type
Operator
- Equality Operator :- “==” :-string1 == string2
3. Comparision Operator :- “> , < ,>= ,<= ”
4. Concatenation Operator ” { } ” : {str1,str2,str3}
5. index operator :- ” [ ] ” : str2 = str1[index]
6. Dot operator : “ . ” : string1.method( )
Functionality
Checks if the two strings are equal. Result is 1 if they are equal and 0 if
they are not. Both strings can be of type string. Or one of them can be a string literal( other data types stores characters).
Checks Inequality of two strings. If not equal returns 1 or if equal returns 0.
Relational operators return 1 if the corresponding condition is true.
It will combine two or more strings to one string. Each operand can be of type string or a string literal(it shall be implicitly converted to type string).
It will returns the character which is present in provided index number .Indexes range from 0 to N-1, where N is the number of characters in the string. If given an index out of range, returns 0
The dot (.) operator is used to invoke a specified method on strings.
Inbuilt methods of string data types
1.len( ) :
len( ) is an inbuilt method of strings which returns the length of the string. It will returns the number of characters of the string
syntax : variable_name.len( );
Example : string emp_name =” John”;
int length;
length = emp_name.len( ); // len( ) will return value 4 to variable length
2. putc( index , string ) :
putc( ) is an inbuilt method of string data types .putc( ) will insert a character which is passed as an argument in the corresponding index provided as argument.
Syntax : variable_name.putc( index , string );
Example :
string emp_name = “SAM “;
string inc = “C”;
emp_name.putc ( 0 , inc ); // Character “C” will be replaced at 0 index. SAM –> CAM
3.getc( index ):
getc( ) is an inbuilt method which returns the character present in the index . Index will be provided as an argument .
Syntax : variable_name.getc(index);
Example :
string emp_name = ” JOHN”;
string char;
char = getc(2); // getc() will return character present in index-2 i.e. H
4.toupper( ) :
toupper( ) is an inbuilt string method which will return the converted upper case characters of the string. The letters(characters) of a string will be converted to Upper case and will be returned. The Original string will remain same.
Syntax : variable_name.toupper( );
Example :
string emp_name = “john ” ;
string char ;
char = emp_name.toupper( );//toupper() will change all small letters to capital letters
5.tolower( );
tolower( ) method will convert and returns all upper case characters of string to lower case characters . it will convert all capital letters to small letters. The Original will not change ,it will remain same.
Syntax : variable_name.tolower( );
Example :
string emp_name = “JOHN”;
string char ;
char = emp_name.tolower( ); //all upper case letters will be converted to lower case
6.compare ( ) :
compare( ) is an inbuilt method of string data type which will compares the length of the two strings and returns value “1” if length is same.
Syntax : variable_name.compare(variable_name);
Example :
string emp_name = ” john “;
string st_name = “blue “;
emp_name.compare(st_name); // compare will return 1 if both are of same length.
7.substr ( index-1 , index-2) :
substr( ) is an inbuilt method of string data type . substr( ) will forms a string by collecting the characters from index-1 to index-2 and the new string will be returned .
Syntax : variable_name.substr(index-1 , index-2);
Example :
string emp_name=”john_victor”;
string sub_string;
sub_string = emp_name.substr(3,8); // substr( ) will return “n_vict “