关于字符串数据类型的示例

描述

字符串数据类型是一个有序的字符集合。

字符串变量的长度是集合中的字符数。

字符串类型的变量是动态的,因为它们的长度在仿真过程中可能会变化。

字符串中的单个字符变量的类型为byte。

Syntax:

 

string variable_name [= initial_value];

 

如果在声明中未指定初始值,则将变量初始化为" ",一个空字符串。空字符串的长度为零。下面是一个字符串数据类型的示例:

 

module datatype1;
 string s1 = "hello";
 string s2 = "hello world";
 string s3 = "helloworld"; //  is ignored
 string s4, s5, s6;
 initial
 begin
 s4 = "later";
 s5 = ""; //empty string
 s6 = {"hi", s5}; //concatenation
 #10;
 $display ("s1=%s s2=%s s3=%s s4=%s s5=%s s6=%s", s1, s2, 
s3, s4, s5, s6);
 #100 $fnish;
 end 
 endmodule

 

在本例中,我们声明了6个字符串,从s1到s6。s1、s2和s3分别进行了初始化。其中字符串s3中的转义字符()被转义为空字符。

然后在“initial”语句块中,将s4赋值为字符串“later”,将s5赋值为空字符串,并连接s5与" hi ",赋值给字符串s6。下面是仿真log:

 

ncsim> run
s1=hello s2=hello world s3=helloworld s4=later s5= s6=hi

 

String Operators

字符串数据类型还提供了处理字符串的操作符,如下表所示:

数据类型

 

module datatype1; 
 string s2 = "hello world"; 
 string s3 = "helloworld"; 
 string s4, s5;
 string s6 = "compare"; 
 string s7 = "compare"; 
 string s8;
 integer s2len, s3len, s2c;
 initial begin
 #10; $display ("s2=%s s3=%s",s2,s3);
 #100 $fnish;
 end
 initial begin
 #15;
 s2len = s2.len( ); $display("String Length s2 = 
%0d",s2len); 
 s3len = s3.len( ); $display("String Length s3 = 
%0d",s3len);
 if (s2 == s3) $display("s2 = s3"); else $display("s2 
!= s3");
 if (s6 == s7) $display("s6 = s7"); else $display("s6 
!= s7");
 s4 = s2.substr(1,6);
 $display("s4 = %s",s4);
 s5 = "later ";
 s8 = {3{s5}};
 $display("s8 = %s",s8);
 S2c = s2[0];
 $display("s2c = %s",s2c);
 end 
 endmodule

 


在上面的例子中,声明了字符串s2到s8并初始化为各种字符串。

将字符串长度(.len())赋值给整数类型s2len和s3len。

比较字符串s2和s3以及字符串s6和
s7。

提取s2的部分字符串并将其赋值给s4。

复制s5三次,赋值给s8。

下面是仿真的log:

 

s2=hello world s3=helloworld
String Length s2 = 11
String Length s3 = 10
s2 != s3
s6 = s7
s4 = ello w
s8 = later later later
s2c = h
$fnish at simulation time 110
 V C S S i m u l a t i o n R e p o r t

 

String Methods

还有几种方法可用于处理字符串,如下表所示:

数据类型

 

module sMethods;
 string s1 = "hello";
 string s2 = "hello world";
 string s4;
 string s5 = "GOODBYE";

 byte x;
 integer s2len, s3len, i1, i2;
 initial
 begin
 #15;
 s2len = s2.len( );
 $display("String Length s2 = %0d",s2len);
 s1.putc(0,"C"); //replace 0'th character with 'C'
 $display("String s1 = %s",s1);
 x = s1.getc(0); //get 0'th character of string s1.
 $display("0'th character of string 'Cello' = %s",x);
 s4 = s2.toupper( ); //convert string 's2' to upper case
 $display("Upper Case of 'hello world' = %s",s4);
 s4 = s5.tolower ( );
 $display("Lower case of 'GOODBYE' = %s",s4);

 end
 endmodule

 

以及仿真log:

 

String Length s2 = 11
String s1 = Cello
0'th character of string 'Cello' = C
Upper Case of 'hello world' = HELLO WORLD
Lower case of 'GOODBYE' = goodbye
 V C S S i m u l a t i o n R e p o r t

 

在上面的例子中,

声明一些字符串变量。在“initial”语句块中,我们首先得到字符串(s2 = " hello world ";)的长度11。

然后使用" putc "方法给字符串" s1 "的第0个字符加上字符" C ":

 

s1.putc(0,“C”);

 

由于s1字符串是“hello”,它现在变成“Cello”。然后,得到字符串s1的第0个字符(现在是“Cello”):

 

x = s1.getc (0);

 

我们使用.toupper方法将" s2 "字符串(s2 = " hello world ")转换为全大写,并将结果存储在字符串" s4 "中:

 

s4 = s2.toupper ();

 

将" s5 "字符串(s5 = " GOODBYE ")转换为全小写(.tolower方法)

 

s4 = s5.tolower ( );

 

  审核编辑:汤梓红

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分