一、Python常用字符串操作函數
Python中常用的字符串操作函數包括:strip()
、split()
、join()
、replace()
等。
strip()
函數用於去除字符串開頭和結尾處的空格;split()
函數可以將一個字符串按照指定的分隔符分割成一個列表;join()
函數可以將一個列表中的元素按照指定的分隔符拼接成一個字符串;replace()
函數可以替換字符串中的子串。
下面是Python字符串操作函數的示例代碼:
#include <iostream> #include <string> using namespace std; int main() { string s = " hello, world! "; cout << "原始字符串:" << s << endl; // 使用strip()函數去除空格 s = s.strip(); cout << "去除空格後的字符串:" << s << endl; // 使用split()函數按照逗號分隔字符串 vector<string> v; string delim = ","; size_t start = 0, end = s.find(delim); while (end != string::npos) { v.push_back(s.substr(start, end - start)); start = end + delim.length(); end = s.find(delim, start); } v.push_back(s.substr(start, end - start)); cout << "分割後的字符串:" << endl; for (int i = 0; i < v.size(); i++) { cout << v[i] << endl; } // 使用join()函數按照逗號拼接字符串 string s_new = ""; for (int i = 0; i < v.size(); i++) { if (i != 0) { s_new += ","; } s_new += v[i]; } cout << "拼接後的字符串:" << s_new << endl; // 使用replace()函數替換字符串 string s_replace = "hello, world!"; s_replace = s_replace.replace(0, 5, "hi"); cout << "替換後的字符串:" << s_replace << endl; return 0; }
二、C++字符串操作函數
C++中有一些內置的字符串操作函數,如strcpy()
、strcat()
、strlen()
、strcmp()
等。
strcpy()
函數用於將一個字符串複製到另一個字符串中;strcat()
函數用於將一個字符串連接到另一個字符串的末尾;strlen()
函數用於獲取字符串的長度;strcmp()
函數用於比較兩個字符串是否相等。
下面是C++字符串操作函數的示例代碼:
#include <iostream> #include <cstring> using namespace std; int main() { char s[] = "hello, world!"; cout << "原始字符串:" << s << endl; // 使用strcpy()函數將字符串複製到另一個字符串中 char s_copy[20]; strcpy(s_copy, s); cout << "複製後的字符串:" << s_copy << endl; // 使用strcat()函數將字符串連接到另一個字符串的末尾 char s_cat[20] = "hi, "; strcat(s_cat, "hello"); cout << "拼接後的字符串:" << s_cat << endl; // 使用strlen()函數獲取字符串的長度 cout << "字符串的長度為:" << strlen(s) << endl; // 使用strcmp()函數比較兩個字符串是否相等 char s1[] = "hello"; char s2[] = "hello"; char s3[] = "world"; if (strcmp(s1, s2) == 0) { cout << "s1 和 s2 相等" << endl; } if (strcmp(s1, s3) != 0) { cout << "s1 和 s3 不相等" << endl; } return 0; }
三、C語言字符串操作函數
C語言中常用的字符串操作函數包括gets()
、puts()
、printf()
、scanf()
等。
gets()
函數用於從標準輸入流讀取一行字符串;puts()
函數用於向標準輸出流輸出一個字符串;printf()
函數用于格式化輸出變量;scanf()
函數用於從標準輸入流讀取數據。
下面是C語言字符串操作函數的示例代碼:
#include <stdio.h> #include <string.h> int main() { char s[20]; printf("請輸入一個字符串:"); gets(s); printf("您輸入的字符串:%s\n", s); char s2[20] = "hello, world!"; puts(s2); int a = 10, b = 20; printf("%d + %d = %d\n", a, b, a + b); int c; scanf("%d", &c); printf("您輸入的數字:%d\n", c); return 0; }
四、字符串操作的常見函數
除了上述語言自帶的字符串操作函數外,還有一些常見的字符串操作函數,如tolower()
、toupper()
、isdigit()
、isalnum()
等。
tolower()
函數用於將一個字符轉換為小寫字母;toupper()
函數用於將一個字符轉換為大寫字母;isdigit()
函數用於判斷一個字符是否為數字字符;isalnum()
函數用於判斷一個字符是否為字母或數字。
下面是字符串操作的常見函數的示例代碼:
#include <iostream> #include <cctype> using namespace std; int main() { char s[] = "Hello, World!"; cout << "原始字符串:" << s << endl; // 使用tolower()函數將字符串轉換為小寫字母 string s_low = ""; for (int i = 0; i < strlen(s); i++) { s_low += tolower(s[i]); } cout << "轉換為小寫字母后的字符串:" << s_low << endl; // 使用toupper()函數將字符串轉換為大寫字母 string s_up = ""; for (int i = 0; i < strlen(s); i++) { s_up += toupper(s[i]); } cout << "轉換為大寫字母后的字符串:" << s_up << endl; // 使用isdigit()函數判斷字符是否為數字字符 char a = '1', b = 'a'; if (isdigit(a)) { cout << a << "是數字字符" << endl; } else { cout << a << "不是數字字符" << endl; } if (isdigit(b)) { cout << b << "是數字字符" << endl; } else { cout << b << "不是數字字符" << endl; } // 使用isalnum()函數判斷字符是否為字母或數字 char c = '1', d = '.'; if (isalnum(c)) { cout << c << "是字母或數字" << endl; } else { cout << c << "不是字母或數字" << endl; } if (isalnum(d)) { cout << d << "是字母或數字" << endl; } else { cout << d << "不是字母或數字" << endl; } return 0; }
五、VBA字符串操作函數
VBA中常用的字符串操作函數包括Len()
、Left()
、Right()
、Mid()
、InStr()
、Replace()
等。
Len()
函數用於獲取字符串的長度;Left()
函數用於從字符串的開頭獲取指定數量的字符;Right()
函數用於從字符串的末尾獲取指定數量的字符;Mid()
函數用於從字符串的指定位置獲取指定數量的字符;InStr()
函數用於查找一個字符串中是否包含另一個字符串;Replace()
函數用於替換字符串中的子串。
下面是VBA字符串操作函數的示例代碼:
Public Sub StringDemo() Dim s As String, s_new As String s = "hello, world!" Debug.Print "原始字符串:" & s ' 使用Len()函數獲取字符串長度 Debug.Print "字符串長度:" & Len(s) ' 使用Left()函數獲取字符串開頭的指定數量的字符 s_new = Left(s, 5) Debug.Print "獲取開頭的指定數量的字符:" & s_new ' 使用Right()函數獲取字符串末尾的指定數量的字符 s_new = Right(s, 6) Debug.Print "獲取末尾的指定數量的字符:" & s_new ' 使用Mid()函數獲取字符串中指定位置的指定數量的字符 s_new = Mid(s, 4, 6) Debug.Print "獲取指定位置的指定數量的字符:" & s_new ' 使用InStr()函數查找字符串中是否包含另一個字符串 Debug.Print "字符串是否包含'world': " & InStr(s, "world") ' 使用Replace()函數替換字符串中的子串 s_new = Replace(s, "hello", "hi") Debug.Print "替換後的字符串:" & s_new End Sub
六、JS字符串操作函數
JS中常用的字符串操作函數包括charAt()
、indexOf()
、slice()
、substring()
、toUpperCase()
、toLowerCase()
等。
charAt()
函數用於返回字符串中指定位置的字符;indexOf()
函數用於返回字符串中指定子串的位置;slice()
函數用於從字符串中獲取指定起始位置和結束位置之間的字符;substring()
函數用於從字符串中獲取指定起始位置和結束位置之間的字符;toUpperCase()
函數用於將字符串轉換為大寫字母;toLowerCase()
函數用於將字符串轉換為小寫字母。
下面是JS字符串操作函數的示例代碼:
var s = "Hello, World!";
console.log("原始字符串:" + s);// 使用charAt()函數獲取指定位置的字符
console.log("第6個字符是:" + s.charAt(5));// 使用indexOf()函數查找子串的位置
console.log("子串'World'的位置是:" + s.indexOf("World"));// 使用slice()函數獲取指定起始和結束位置之間的字符
console.log("截取子串:" + s.slice(6, 11));// 使用substring()函數獲取指定起始和結束位置之間的字符
console.log("截取子串:" + s.substring(6, 11));// 使用toUpperCase()函數將字符串轉換為大寫字母
console.log("轉換原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/227476.html