複製鏈接
請複製以下鏈接發送給好友

字符串函數

鎖定
字符串函數(String processing function)也叫字符串處理函數,指的是編程語言中用來進行字符串處理的函數,如C,pascal,Visual以及LotusScript中進行字符串拷貝,計算長度,字符查找等的函數。
中文名
字符串函數
外文名
Stringprocessingfunction

字符串函數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: %c\n", *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

字符串函數定位函數

字符串函數截取函數

Mid,Right,Left

字符串函數替換函數

Replace

字符串函數分割函數

Split

字符串函數格式化輸出

Format

字符串函數比較函數

StrComp,Like

字符串函數長度計算

Len,LenB

字符串函數編碼轉換

字符串函數大小寫轉換

LCase,UCase

字符串函數重複產生

String,Space

字符串函數重排字符串

LSet,RSet

字符串函數空白清理函數

LTrim,RTrim,Trim

字符串函數ASCII碼

Asc,Chr,AscB,ChrB

字符串函數LotusScript

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