C++常用函数:字符串操作函数
一、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("转换
版权声明
本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。
上一篇:C++遍历二维数组:实现数组元素的有效访问 下一篇:掌握C++运算符重载技巧
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。