LR中常用的C函數


注意:
將字符串中的數字轉換為浮點數的函數,如果不事先聲明,則轉換有問題。
1       strcat

char *strcat ( char *to, const char *from );

功能:鏈接兩個字符串。

例子:

這個例子是用strcat鏈接字符串:zee和slo@hotmail.co

腳本如下:

     char test[1024], *a = "slo@hotmail.com";

     strcpy(test, "zee");

     strcat(test, a);

     lr_output_message("We can see %s",test);


運行后在executon log中看到如下語句:

Starting action Action.
Action.c(16): We can see zeeslo@hotmail.com

2       strchr
char *strchr ( const char *string, int c );

功能:返回字符串中指定字符后面的字符串。

例子:

這個例子是返回第一個出現e字符以后所有的字符,和最后一次出現e字符以后所有的字符。

腳本如下:
     char *string = "Zee is a tester";
     char *first_e, *last_e;

     first_e = (char *)strchr(string, 'e');
     lr_output_message("We can see the first occurrence of e: %s",first_e);
     last_e = (char *)strrchr(string, 'e');
     lr_output_message("We can see the last occurrence of e: %s", last_e);

運行后在executon log中看到如下語句:
Starting action Action.
Action.c(12): We can see the first occurrence of e: ee is a tester
Action.c(14): We can see the last occurrence of e: er
3       Strcmp&stricmp
int strcmp ( constchar *string1, const char *string2 );大小寫敏感。
int stricmp ( const char *string1, const char *string2 );大小寫不敏感。

功能:比較字符串。

例子:

按是否區分大小寫對比兩個字符串,并打印出它們的大小關系。

腳本如下:
     int result;
     char tmp[20];
     char string1[] = "We can see the string:ZEE";
     char string2[] = "We can see the string:zee";

     result = strcmp( string1, string2 ); /*區分大小寫,比較字符串 */

     if( result > 0 )
          strcpy( tmp, "大于" );
     else if( result < 0 )
          strcpy( tmp, "小于" );
     else
          strcpy( tmp, "等于" );

     lr_output_message( "strcmp: String 1 %s string 2", tmp );

     result = stricmp( string1, string2 ); /* 不區分大小寫,比較字符串 */

     if( result > 0 )
          strcpy( tmp, "大于" );
     else if( result < 0 )
          strcpy( tmp, "小于" );
     else
          strcpy( tmp, "等于" );

     lr_output_message( "stricmp: String 1 %s string 2", tmp );    

運行后在executon log中看到如下語句:
Starting action Action.
Action.c(22): strcmp: String 1 小于 string 2
Action.c(33): stricmp: String 1 等于 string 2
  
4       strcpy
char *strcpy ( char *dest, const char *source );

功能:復制一個字符串到另一個字符串中。

例子:

復制一個字符串到字符數組中,并打印出來。

腳本如下:

     char test[1024];

     strcpy(test, "what can we see?       ");

     lr_output_message("%s", test);

運行后在executon log中看到如下語句:
Starting action Action.
Action.c(10): what can we see?      
5       Strdup& strlwr
char *strdup ( const char *string );

復制一個字符串。

char *strlwr ( char *string );

轉換成小寫字母。

例子:

在這個例子中,Vuser的組名被轉換為小寫字母。但是lr_whoami把組名作為靜態buffer返回。這樣的buffer不能被操作。如果有操作需要,就復制這個靜態buffer。

腳本如下:

     int id;
     char *groupname_static, *groupname;

     /* 從VuGen中得到組名 */
     lr_whoami(&id, &groupname_static, NULL);
     lr_output_message("groupname=%s", groupname_static);

     /*復制這個靜態組名以便我們可以操作它 */
     groupname = (char *)strdup(groupname_static);
     groupname = (char *)strlwr(groupname);
     lr_output_message("lower case groupname=%s", groupname);

     free(groupname);
上述腳本用vugen保存為:CHANGE

在controller中運行(設置為總是發送消息)
運行后在log中看到如下語句:
Starting action Action.    [MsgId: MMSG-15919]
Action.c(11): groupname=CHANGE     [MsgId: MMSG-17999]
Action.c(16): lower case groupname=change      [MsgId: MMSG-17999]
6       Strlen
size_t strlen ( constchar *string );

功能:返回字符串長度(bytes).

例子:

這個例子很簡單,就是得到一個字符串中的字符的個數。然后打印出來。

腳本如下:

     char *str = "Zee is a tester";
     unsigned int len;

     len = strlen(str);

     lr_output_message("The sentence has %d letters",len);

運行后在log中看到如下語句:
Action.c(13): The sentence has 15 letters
7       Strncat
char *strncat ( char *to_string, const char *from_string, size_t n );

把一個字符串連接到另一個字符串后面。

例子:

在這里,我隨便寫了兩個字符串,用此函數把他們連接起來,并打印出來。

腳本如下:


   char str1[]="Zee is ";
    char str2[]="a tester.";
    lr_output_message("What can we see?");
    lr_output_message("The str1 is %s.",str1);

    strncat(str1,str2,20);
lr_output_message("The str1 is %s.",str1);


運行后在log中看到如下語句:
Action.c(9): What can we see?
Action.c(10): The str1 is Zee is .
Action.c(13): The str1 is Zee is a tester..
注:我們可以看到,沒有連接前的str1是:Zee is,連接后的字符串是:Zee is a tester。也可以看看strcat函數。
8       strncmp
int strncmp ( constchar *string1, const char *string2, size_t n );

對比兩個字符串的前n位。

例子:

對比兩個字符串,并把對比結果打印出來。這里我和上面的strcmp一起寫。

腳本如下:

    char result;
    char str1[]="Zee is a tester.";
    char str2[]="Zee is a tester.";
    char str3[]="zee is a tester?";
   
    result = strcmp(str1,str2);

    if(result > 0)
         lr_output_message("str1 is greater than str2.");
    else if(result < 0)
         lr_output_message("str1 is less than str2.");
    else
         lr_output_message("str1 is equal to str2.");
  
    result = strncmp( str1, str3 , 30);


    if(result > 0)
         lr_output_message("str1 is greater than str3.");
    else if(result < 0)
         lr_output_message("str1 is less than str3.");
    else
         lr_output_message("str1 is equal to str3.");

運行后在log中看到如下語句:
Starting iteration 1.
Starting action Action.
Action.c(18): str1 is equal to str2.
Action.c(28): str1 is less than str3.