一、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-tw/n/227476.html