字元串主要用於編程,字元串在存儲上類似字元數組,所以它每一位的單個元素都是可以提取的。字元串也有很多操作,在正文將對C語言、C++和java中對其操作進行介紹。
基本介紹
名詞介紹
套用
功能:拷貝一個字元串到另一個。
#include <stdio.h> #include <string.h>int main(void) { char string[10]; char *str1 = "abcdefghi"; stpcpy(string, str1); printf("%s\n", string); return 0; }
用法:char *strcat(char *destin, char *source);
舉例如下:
#include <string.h> #include <stdio.h>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; }
用法::char *strchr(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 = 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; }
用法:int strcmp(char *str1, char *str2);
看Asic碼,str1>str2,返回值 > 0;兩串相等,返回0。
#include <string.h> #include <stdio.h>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; }
用法:char *strcpy(char *str1, char *str2);
#include <stdio.h> #include <string.h>int main(void) { char string[10]; char *str1 = "abcdefghi"; strcpy(string, str1); printf("%s\n", string); return 0; }
用法::char *strerror(int errnum);
#include <stdio.h> #include <errno.h>int main(void) { char *buffer; buffer = strerror(errno); printf("Error: %s\n", buffer); return 0; }
用法:char *strnset(char *str, char ch, unsigned n);
#include <stdio.h> #include <string.h>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; }
用法: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 set\n"); return 0; }
用法:char *strrev(char *str);
#include <string.h> #include <stdio.h>int main(void) { char *forward = "string"; printf("Before strrev(): %s\n", forward); strrev(forward); printf("After strrev(): %s\n", forward); return 0; }
用法: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 %lf\n", input, value); return 0; }
用法: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 = %ld\n", string, lnumber); return 0; }
用法: 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("%s\n", ptr); return 0; }
用法: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: %s\n", target); return 0; }
# include <iostream># include <string>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<<str4<<endl; getchar(); return 0; }
# include <iostream># include <string>using namespace std;int main(){ string str1 = "yesterday once more"; string str2 ("my heart go on"); str2.swap(str1); cout<<str1<<endl; // = my heart go on cout<<str2<<endl; // = yesterday once more getchar(); return 0;}
s+=”my name is jiayp”;//加個C字元串 s+=’a’;//加個字元 s.append(str); s.append(str,1,3);//不解釋了同前面的函式參數assign的解釋s.append(str,2,string::npos)//不解釋了 s.append(“my name is jiayp”); s.append(“nico”,5); s.append(5,’x’); s.push_back(‘a’);//這個函式只能增加單個字元
s.insert(0,”my name”); s.insert(1,str);
5、字元串刪除
s.erase(13);//從索引13開始往後全刪除 s.erase(7,5);//從索引7開始往後刪5個
string s=”il8n”; s.replace(1,2,”nternationalizatio”);//從索引1開始的2個替換成後面的 C_string s = internationalization
C ++字元串支持常見的比較操作符(>,>=,<,<=,==,!=),甚至支持string與C-string的比較(如 str<”hello”)。在使用>,>=,<,<=這些操作符的時候是根據“當前字元特性”將字元按字典順序進行逐一得比較。字典排序靠前的字元小,比較的順序是從前向後比較,遇到不相等的字元就按這個位置上的兩個字元的比較結果確定兩個字元串的大小。
另一個功能強大的比較函式是成員函式compare()。他支持多參數處理,支持用索引值和長度定位子串來進行比較。他返回一個整數來表示比較結果,返回值意義如下:0-相等 >0-大於 <0-小於。舉例如下:
string s(“abcd”); s.compare(“abcd”); //返回0 s.compare(“dcba”); //返回一個小於0的值 s.compare(“ab”); //返回大於0的值 s.compare(s); //相等 s.compare(0,2,s,2,2); //用”ab”和”cd”進行比較小於零 s.compare(1,2,”bcx”,2); //用”bc”和”bc”比較。
可以使用下標操作符[]和函式at()對元素包含的字元進行訪問。但是應該注意的是操作符[]並不檢查索引是否有效(有效索引0~str.length()),如果索引失效,會引起未定義的行為。而at()會檢查,如果使用 at()的時候索引無效,會拋出out_of_range異常。
string str1 = "Iphone 5";cout<<str1[2]<<endl; // = h cout<<str1.at(4)<<endl; // = nstring stuff;getline(cin,stuff); // 輸入一行字元賦值給stuff getline(cin,stuff,'!'); // 輸入一行字元以“!”結束 cout<<stuff<<endl;
find()
rfind()
find_first_of()
find_last_of()
find_first_not_of()
find_last_not_of()
這些函式返回符合搜尋條件的字元區間內的第一個字元的索引,沒找到目標就返回npos。所有的函式的參數說明如下:
第一個參數是被搜尋的對象。第二個參數(可有可無)指出string內的搜尋起點索引,第三個參數(可有可無)指出搜尋的字元個數。
var num= 19; // 19var myStr = num.toString(); // "19"
var num= 19; // 19var myStr = String(num); // "19"
var num= 19; // 19var myStr = "" +num; // "19"
var myStr = "I,Love,You,Do,you,love,me";var substrArray = myStr .split(","); // ["I", "Love", "You", "Do", "you", "love", "me"];var arrayLimited = myStr .split(",", 3); // ["I", "Love", "You"];
var myStr = "I,Love,You,Do,you,love,me";var myStrLength = myStr.length; //25
var myStr = "I,Love,you,Do,you,love,me";var index = myStr.indexOf("you"); // 7 ,基於0開始,找不到返回-1
var myStr = "I,Love,you,Do,you,love,me";var index = myStr.lastIndexOf("you"); // 14
var myStr = "I,love,you,Do,you,love,me";var replacedStr = myStr.replace("love","hate");//"I,hate,you,Do,you,love,me"
var myStr = "I,love,you,Do,you,love,me";var replacedStr = myStr.replace(/love/g,"hate");//"I,hate,you,Do,you,hate,me"
var myStr = "I,love,you,Do,you,love,me";var theChar = myStr.charAt(8);// "o",同樣從0開始
var myStr = "I,love,you,Do,you,love,me";var theChar = myStr.charCodeAt(8); //111
var str1 = "I,love,you!";var str2 = "Do,you,love,me?";var str = str1 + str2 + "Yes!";//"I,love,you!Do,you,love,me?Yes!"
var str1 = "I,love,you!";var str2 = "Do,you,love,me?";var str = str1.concat(str2);//"I,love,you!Do,you,love,me?"
var myStr = "I,love,you,Do,you,love,me";var subStr = myStr.slice(1,5);//",lov"
var myStr = "I,love,you,Do,you,love,me";var subStr = myStr.substring(1,5); //",love"
var myStr = "I,love,you,Do,you,love,me";var subStr = myStr.substr(1,5); //",love"
var myStr = "I,love,you,Do,you,love,me";var lowCaseStr = myStr.toLowerCase();//"i,love,you,do,you,love,me";var upCaseStr = myStr.toUpperCase();//"I,LOVE,YOU,DO,YOU,LOVE,ME"
var myStr = "I,love,you,Do,you,love,me";var pattern = /love/;var result = myStr.match(pattern);//["love"]console.log(result .index);//2console.log(result.input );//I,love,you,Do,you,love,me
var myStr = "I,love,you,Do,you,love,me";var pattern = /love/;var result = pattern .exec(myStr);//["love"]console.log(result .index);//2console.log(result.input );//I,love,you,Do,you,love,me
var myStr = "I,love,you,Do,you,love,me";var pattern = /love/;var result = myStr.search(pattern);//2
var myStr = "chicken";var myStrTwo = "egg";var first = myStr.localeCompare(myStrTwo); // -1first = myStr.localeCompare("chicken"); // 0first = myStr.localeCompare("apple"); // 1