版权归原作者所有,如有侵权,请联系我们

[科普中国]-字符串操作

科学百科
原创
科学百科为用户提供权威科普内容,打造知识科普阵地
收藏

字符串或串(String)是由数字、字母、下划线组成的一串字符。它是编程语言中表示文本的数据类型。在程序设计中,字符串为符号或数值的一个连续序列。字符串操作就是以串的整体作为操作对象,如:在串中查找某个子串、求取一个子串、在串的某个位置上插入一个子串以及删除一个子串等。

对于字符串的操作方法,在这里通过介绍C语言、C++和java这三种常用的语言来说明。

C语言中字符串操作1、stpcpy()
功能:拷贝一个字符串到另一个。

用法: char *stpcpy(char *destin, char *source);

举例如下:

#include #include int main(void) { char string[10]; char *str1 = "abcdefghi"; stpcpy(string, str1); printf("%s\n", string); return 0; } 2、strcat ()

功能: 字符串拼接函数。
用法:char *strcat(char *destin, char *source);
举例如下:

#include #include int main(void) { char destination[25]; char *blank = " ", *c = "C++", *Borland = "Borland"; strcpy(destination, Borland); strcat(destination, blank); strcat(destination, c); printf("%s\n", destination); return 0; }3、strchr ()

功能: 在一个串中查找给定字符的第一个匹配之处。
用法::char *strchr(char *str, char c);
举例如下:

#include #include int main(void) { char string[15]; char *ptr, c = 'r'; strcpy(string, "This is a string"); ptr = strchr(string, c); if (ptr) printf("The character %c is at position: %d\n", c, ptr-string); else printf("The character was not found\n"); return 0; } 4、 strcmp()

功能:串比较 。
用法:int strcmp(char *str1, char *str2);
看Asic码,str1>str2,返回值 > 0;两串相等,返回0。

举例如下:

#include #include int main(void) { char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc"; int ptr; ptr = strcmp(buf2, buf1); if (ptr > 0) printf("buffer 2 is greater than buffer 1\n"); else printf("buffer 2 is less than buffer 1\n"); ptr = strcmp(buf2, buf3); if (ptr > 0) printf("buffer 2 is greater than buffer 3\n"); else printf("buffer 2 is less than buffer 3\n"); return 0; } 5、strcpy()

功能:串拷贝。
用法:char *strcpy(char *str1, char *str2);

举例如下:

#include #include int main(void) { char string[10]; char *str1 = "abcdefghi"; strcpy(string, str1); printf("%s\n", string); return 0; } 6、strerror()

功能:返回指向错误信息字符串的指针。
用法::char *strerror(int errnum);

举例如下:

#include #include int main(void) { char *buffer; buffer = strerror(errno); printf("Error: %s\n", buffer); return 0; } 7、strnset()

功能: 将一个串中的所有字符都设为指定字符。
用法:char *strnset(char *str, char ch, unsigned n);

举例如下:

#include #include int main(void) { char *string = "abcdefghijklmnopqrstuvwxyz"; char letter = 'x'; printf("string before strnset: %s\n", string); strnset(string, letter, 13); printf("string after strnset: %s\n", string); return 0; } 8、strpbrk()

功能:在串中查找给定字符集中的字符。
用法:char *strpbrk(char *str1, char *str2);

举例如下:

#include #include 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 set\n"); return 0; } 9、strrev()

功能:串倒转。
用法:char *strrev(char *str);

举例如下:

#include #include int main(void) { char *forward = "string"; printf("Before strrev(): %s\n", forward); strrev(forward); printf("After strrev(): %s\n", forward); return 0; } 10、strtod ()

功能:将字符串转换为double型值。
用法:double strtod(char *str, char **endptr);

举例如下:

#include #include 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 %lf\n", input, value); return 0; }11、 strtol()

功能:将串转换为长整数。
用法:long strtol(char *str, char **endptr, int base);

举例如下:

#include #include int main(void) { char *string = "87654321", *endptr; long lnumber; /* strtol converts string to long integer */ lnumber = strtol(string, &endptr, 10); printf("string = %s long = %ld\n", string, lnumber); return 0; } 12、 strupr()

功能:将串中的小写字母转换为大写字母。
用法: char *strupr(char *str);
举例如下:

#include #include int main(void) { char *string = "abcdefghijklmnopqrstuvwxyz", *ptr; /* converts string to upper case characters */ ptr = strupr(string); printf("%s\n", ptr); return 0; } 13、swab()

功能:交换字节1。
用法:void swab (char *from, char *to, int nbytes);

举例如下:

#include #include #include char source[15] = "rFna koBlrna d"; char target[15];int main(void) { swab(source, target, strlen(source)); printf("This is target: %s\n", target); return 0; }C++字符串操作选用C++标准程序库中的string类,是因为他和c-string比较起来,不必担心内存是否足够、字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下的需要。

1、字符串赋值

使用=,assign() ,“=”的用法不作详细说明,assign用法如下:

# include # include using namespace std;int main(){ string str1 = "yesterday once more"; string str2 ("my heart go on"); string str3,str4; str3.assign(str2,3,6); // = heart str4.assign(str2,3,string::npos); // = heart go on (从2开始到结尾赋给str4) str4.assign("gaint"); // =gaint str4.assign("nico",5); // = nico str4.assign(5,'x'); // cout