字元串函式

字元串函式

字元串函式(String processing function)也叫字元串處理函式,指的是程式語言中用來進行字元串處理的函式,如C,pascal,Visual以及LotusScript中進行字元串拷貝,計算長度,字元查找等的函式。

基本介紹

  • 中文名:字元串函式
  • 外文名:String processing function
c,strcpy,strcat,strlen,strncat,strncpy,strcspn,strdup,stricmp,strerror,strcmp,strncmp,strnicmp,strnset,strpbrk,strrchr,strrev,strset,strspn,strstr,strtod,strtok,strtol,strupr,swab,isalpha,pascal,Visual Basic,定位函式,截取函式,替換函式,分割函式,格式化輸出,比較函式,長度計算,編碼轉換,大小寫轉換,重複產生,重排字元串,空白清理函式,字元ASCII碼,LotusScript,

c

strcpy

原型:extern char *strcpy(char *dest,char *src);
用法:#include <string.h>
功能:把src所指由NUL結束的字元串複製到dest所指的數組中。
返回指向dest結尾處字元(NUL)的指針
舉例:
// strcpy.c
#include <syslib.h>
#include <string.h>
main()
{
char *s="Golden Global View";
char d[20];
clrscr();
strcpy(d,s);
printf("%s",d);
getchar();
return 0;
}

strcat

原型:extern char *strcat(char *dest,char *src);
用法:#include <string.h>
功能:把src所指字元串添加到dest結尾處(覆蓋dest結尾處的'\0')並添加'\0'。
返回指向dest的指針
舉例:
// strcat.c
#include <syslib.h>
#include <string.h>
main()
{
char d[20]="Golden Global";
char *s=" View";
clrscr();
strcat(d,s);
printf("%s",d);
getchar();
return 0;
}

strlen

原型:extern int strlen(char *s);
用法:#include <string.h>
功能:計算字元串s的長度
說明:返回s的長度,不包括結束符NULL。
舉例:
// strlen.c
#include <syslib.h>
#include <string.h>
main()
{
char *s="Golden Global View";
clrscr();
printf("%s has %d chars",s,strlen(s));
getchar();
return 0;
}

strncat

原型:extern char *strncat(char *dest,char *src,int n);
用法:#include <string.h>
功能:把src所指字元串的前n個字元添加到dest結尾處(覆蓋dest結尾處的'\0')並添加'\0'。
返回指向dest的指針。
舉例:
// strncat.c
#include <syslib.h>
#include <string.h>
main()
{
char d[20]="Golden Global";
char *s=" View WinIDE Library";
clrscr();
strncat(d,s,5);
printf("%s",d);
getchar();
return 0;
}

strncpy

原型:extern char *strncpy(char *dest, char *src, int n);
用法:#include <string.h>
功能:把src所指由NULL結束的字元串的前n個位元組複製到dest所指的數組中。
說明:
如果src的前n個位元組不含NULL字元,則結果不會以NULL字元結束。
如果src的長度小於n個位元組,則以NULL填充dest直到複製完n個位元組。
src和dest所指記憶體區域不可以重疊且dest必須有足夠的空間來容納src的字元串。
返回指向dest的指針。
舉例:
// strncpy.c
#include <syslib.h>
#include <string.h>
main()
{
char *s="Golden Global View";
char *d="Hello, GGV Programmers";
char *p=strdup(s);
clrscr();
textmode(0x00); // enable 6 lines mode
strncpy(d,s,strlen(s));
printf("%s\n",d);
strncpy(p,s,strlen(d));
printf("%s",p);
free(p);
getchar();
return 0;
}

strcspn

功 能: 在串中查找第一個給定字元集內容的段
用 法: int strcspn(char *str1, char *str2);
程式例:
#include <stdio.h>
#include <string.h>
#include <alloc.h>
int main(void)
{
char *string1 = "1234567890";
char *string2 = "747DC8";
int length;
length = strcspn(string1, string2);
printf("Character where strings intersect is at position %dn", length);
return 0;
}

strdup

功 能: 將串拷貝到新建的位置處
用 法: char *strdup(char *str);
程式例:
#include <stdio.h>
#include <string.h>
#include <alloc.h>
int main(void)
{
char *dup_str, *string = "abcde";
dup_str = strdup(string);
printf("%sn", dup_str);
free(dup_str);
return 0;
}

stricmp

功 能: 以大小寫不敏感方式比較兩個串
用 法: int stricmp(char *str1, char *str2);
程式例:
#include <string.h>
#include <stdio.h>
int main(void)
{
char *buf1 = "BBB", *buf2 = "bbb";
int ptr;
ptr = stricmp(buf2, buf1);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1n");
if (ptr < 0)
printf("buffer 2 is less than buffer 1n");
if (ptr == 0)
printf("buffer 2 equals buffer 1n");
return 0;
}
注意:此函式為Windows特有,在其他環境下可能會無法使用。

strerror

功 能: 返回指向錯誤信息字元串的指針
用 法: char *strerror(int errnum);
程式例:
#include <stdio.h>
#include <errno.h>
int main(void)
{
char *buffer;
buffer = strerror(errno);
printf("Error: %sn", buffer);
return 0;
}

strcmp

功 能: 將一個串與另一個比較
用 法: int strcmp(char *str1, char *str2);
程式例:
#include <string.h>
#include <stdio.h>
int main(void)
{
char *buf1 = "BBB", *buf2 = "bbb";
int ptr;
ptr =strcmp(buf2, buf1);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1n");
if (ptr < 0)
printf("buffer 2 is less than buffer 1n");
if (ptr == 0)
printf("buffer 2 equals buffer 1n");
return 0;
}

strncmp

功 能: 把串中的一部分與另一串中的一部分比較 (前n個字元)
用 法: int strncmp(char *str1, char *str2,int maxlen);
程式例:
#include <string.h>
#include <stdio.h>
int main(void)
{
char *buf1 = "BBBccc", *buf2 = "bbbccc";
int ptr;
ptr = strncmp(buf2,buf1,3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1n");
if (ptr < 0)
printf("buffer 2 is less than buffer 1n");
if (ptr == 0)
printf("buffer 2 equals buffer 1n");
return 0;
}

strnicmp

功 能: 不注重大小寫地比較兩個串的前n個字元
用 法: intstrnicmp(char *str1, char *str2, unsigned maxlen);
程式例:
#include <string.h>
#include <stdio.h>
int main(void)
{
char *buf1 = "BBBccc", *buf2 = "bbbccc";
int ptr;
ptr =strnicmp(buf2, buf1, 3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1n");
if (ptr < 0)
printf("buffer 2 is less than buffer 1n");
if (ptr == 0)
printf("buffer 2 equals buffer 1n");
return 0;
}

strnset

功 能: 將一個串中的所有字元都設為指定字元
用 法: char *strnset(char *str, char ch, unsigned n);
程式例:
#include <stdio.h>
#include <string.h>
int main(void)
{
char letter = 'x';
printf("string beforestrnset: %sn", string);
strnset(string, letter, 13);
printf("string afterstrnset: %sn", string);
return 0;
}

strpbrk

功 能: 在串中查找給定字元集中的字元
用 法: char *strpbrk(char *str1, char *str2);
程式例:
#include <stdio.h>
#include <string.h>
int main(void)
{
char *string1 = "abcdefghijklmnopqrstuvwxyz";
char *string2 = "onm";
char *ptr;
ptr = strpbrk(string1, string2);
if (ptr)
printf("strpbrk found first character: %cn", *ptr);
else
printf("strpbrk didn't find character in setn");
return 0;
}

strrchr

功 能: 在串中查找指定字元的最後一個出現
用 法: char *strrchr(char *str, char c);
程式例:
#include <string.h>
#include <stdio.h>
int main(void)
{
char string[15];
char *ptr, c = 'r';
strcpy(string, "This is a string");
ptr = strrchr(string, c);
if (ptr)
printf("The character %c is at position: %dn", c, ptr-string);
else
printf("The character was not foundn");
return 0;
}

strrev

功 能: 串倒轉
用 法: char *strrev(char *str);
程式例:
#include <string.h>
#include <stdio.h>
int main(void)
{
char *forward = "string";
printf("Before strrev(): %sn", forward);
strrev(forward);
printf("After strrev(): %sn", forward);
return 0;
}

strset

功 能: 將一個串中的所有字元都設為指定字元
用 法: char *strset(char *str, char c);
程式例:
#include <stdio.h>
#include <string.h>
int main(void)
{
char string[10] = "123456789";
char symbol = 'c';
printf("Beforestrset(): %sn", string);
strset(string, symbol);
printf("Afterstrset(): %sn", string);
return 0;
}

strspn

功 能: 返回字元串中第一個不在指定字元串中出現的字元下標
用 法: int strspn(char *str1, char *str2);
程式例:
#include <stdio.h>
#include <string.h>
#include <alloc.h>
int main(void)
{
char *string1 = "1234567890";
char *string2 = "123DC8";
int length;
length = strspn(string1, string2);
printf("Character where strings differ is at position %dn", length);
return 0;
}

strstr

功 能: 在串中查找指定字元串的第一次出現
用 法: char *strstr(char *str1, char *str2);
程式例:
#include <stdio.h>
#include <string.h>
int main(void)
{
char *str1 = "Borland International", *str2 = "nation", *ptr;
ptr = strstr(str1, str2);
printf("The substring is: %sn", ptr);
return 0;
}

strtod

功 能: 將字元串轉換為double型值
用 法: double strtod(char *str, char **endptr);
程式例:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char input[80], *endptr;
double value;
printf("Enter a floating point number:");
gets(input);
value = strtod(input, &endptr);
printf("The string is %s the number is %lfn", input, value);
return 0;
}

strtok

功 能: 查找由在第二個串中指定的分界符分隔開的單詞
用 法: char *strtok(char *str1, char *str2);
程式例:
#include <string.h>
#include <stdio.h>
int main(void)
{
char input[16] = "abc,d";
char *p;
/* strtok places a NULL terminator
in front of the token, if found */
p = strtok(input, ",");
if (p) printf("%sn", p);
/* A second call to strtok using a NULL
as the first parameter returns a pointer
to the character following the token */
p = strtok(NULL, ",");
if (p) printf("%sn", p);
return 0;
}

strtol

功 能: 將串轉換為長整數
用 法: long strtol(char *str, char **endptr, int base);
程式例:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char *string = "87654321", *endptr;
long lnumber;
/* strtol converts string to long integer */
lnumber = strtol(string, &endptr, 10);
printf("string = %s long = %ldn", string, lnumber);
return 0;
}

strupr

功 能: 將串中的小寫字母轉換為大寫字母
用 法: char *strupr(char *str);
程式例:
#include <stdio.h>
#include <string.h>
int main(void)
{
char *string = "abcdefghijklmnopqrstuvwxyz", *ptr;
/* converts string to upper case characters */
ptr =strupr(string);
printf("%sn", ptr);
return 0;
}

swab

功 能: 交換位元組
用 法: void swab (char *from, char *to, int nbytes);
程式例:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char source[15] = "rFna koBlrna d";
char target[15];
int main(void)
{
swab(source, target, strlen(source));
printf("This is target: %sn", target);
return 0;
}
PS:isalpha()是字元函式,不是字元串函式,

isalpha

原型:extern int isalpha(int c);
用法:#include <ctype.h>
功能:判斷字元c是否為英文字母
說明:當c為英文字母a-z或A-Z時,返回非零值,否則返回零。
舉例:
// isalpha.c
#include <syslib.h>
#include <ctype.h>
#include <stdio.h>
main()
{
int c;
clrscr(); // clear screen
printf("Press a key");
for(;;)
{
c=getchar();
clrscr();
printf("%c: %s letter",c,isalpha(c)?"is":"not");
}
return 0; // just to avoid warnings by compiler
}

pascal

1. 連線運算 concat(s1,s2,s3…sn) 相當於s1+s2+s3+…+sn.
例:concat('11','aa')='11aa';
2. 求子串。 Copy(s,I,L) 從字元串s中截取第I個字元開始後的長度為l的子串。
例:copy('abdag',2,3)='bda'
3. 刪除子串。過程 Delete(s,I,l) 從字元串s中刪除第I個字元開始後的長度為l的子串。
delete(大串。開始位置,個數)刪除子串
例:s:='abcde';delete(s,2,3);結果s:='ae'
delete('1234567',5,2)='12347'
delete('abcded123',1,1)='bcded123'
4. 插入子串。 過程Insert(s1,s2,I) 把s1插入到s2的第I個位置
例:s:=abc;insert('12',s,2);結果s:='a12bc'
5. 求字元串長度 length(s) 例:length('12abc')=5
6. 搜尋子串的位置 pos(s1,s2) 如果s1是s2的子串 ,則返回s1的第一個字元在s2中的位置,若不是子串,則返回0.
例:pos('ab','12abcd')=3
7. 字元的大寫轉換。Upcase(ch) 求字元ch的大寫體。
例:upcase('a')='A'
8. 數值轉換為數串。 過程 Str(x,s) 把數值x化為數串s.
例:str(12345,s); 結果s='12345'
9. 數串轉換為數值。 過程val(s,x,I) 把數串s轉化為數值x,如果成功則l=0,不成功則I為無效字元的序數
例:val('1234',x,I);結果 x:=1234
10.求字元序號。ord(a)求char型字元a的ASCII碼。
例:ord('A')=65

Visual Basic

定位函式

截取函式

替換函式

分割函式

格式化輸出

比較函式

長度計算

編碼轉換

大小寫轉換

重複產生

重排字元串

LSet,RSet

空白清理函式

字元ASCII碼

Asc,Chr,AscB,ChrB

LotusScript

LotusScript里的字元串處理函式在截取子字元串方面很方便好用。在VBScript、JavaScript、LotusScript三種腳本語言中,LotusScript的最全面實用。
函式
功能
Left
截取字元串最左邊的指定長度的子字元串。
Right
截取字元串最右邊的指定長度的子字元串。
Mid
截取字元串指定位置起的指定長度的子字元串。
Strleft
在字元串S1里從左往右查找字元串S2,返回S1中位於S2左邊的子字元串。
Strleftback
在字元串S1里從右往左查找字元串S2,返回S1中位於S2左邊的子字元串。
Strright
在字元串S1里從左往右查找字元串S2,返回S1中位於S2右邊的子字元串。
Strrightback
在字元串S1里從右往左查找字元串S2,返回S1中位於S2右邊的子字元串。

相關詞條

熱門詞條

聯絡我們