当前位置: 首页 > news >正文

c/c++字符串处理标准库 string 介绍

c语言中string.h介绍

C语言的标准库中包含了一个头文件 <string.h>,该头文件提供了一系列字符串处理函数的声明和定义。以下是一些常用的函数:

  • 字符串复制strcpy(dest, src)。将源字符串 src 复制到目标字符串 dest,包括字符串结束符 \0
#include <stdio.h>
#include <string.h>int main() {char source[] = "Hello, world!";char destination[20];strcpy(destination, source);printf("Source: %s\n", source);printf("Destination: %s\n", destination);return 0;
}

输出结果:

Source: Hello, world!
Destination: Hello, world!

  • 字符串连接strcat(dest, src)。将源字符串 src 连接到目标字符串 dest 的末尾,结果保存在 dest 中,同时返回 dest
#include <stdio.h>
#include <string.h>int main() {char destination[20] = "Hello";char source[] = ", world!";strcat(destination, source);printf("String after concatenation: %s\n", destination);return 0;
}

输出结果:

String after concatenation: Hello, world!

  • 字符串比较strcmp(str1, str2)。比较字符串 str1str2,返回一个整数值表示比较结果。如果返回值为负数,则 str1 小于 str2;如果返回值为正数,则 str1 大于 str2;如果返回值为 0,则 str1 等于 str2
#include <stdio.h>
#include <string.h>int main() {char str1[] = "apple";char str2[] = "banana";int result = strcmp(str1, str2);if (result < 0) {printf("%s is less than %s\n", str1, str2);} else if (result > 0) {printf("%s is greater than %s\n", str1, str2);} else {printf("%s is equal to %s\n", str1, str2);}return 0;
}

输出结果:

apple is less than banana

  • 字符串长度strlen(str)。返回字符串 str 的长度,不包括字符串结束符 \0
#include <stdio.h>
#include <string.h>int main() {char str[] = "Hello, world!";int length = strlen(str);printf("Length of the string: %d\n", length);return 0;
}

输出结果:

Length of the string: 13

  • 字符串查找strchr(str, ch)。在字符串 str 中查找字符 ch 的第一次出现,并返回该字符的指针。如果未找到字符,则返回 NULL
#include <stdio.h>
#include <string.h>int main() {char str[] = "Hello, world!";char ch = 'o';char* result = strchr(str, ch);if (result != NULL) {printf("Character '%c' found at position: %ld\n", ch, result - str);} else {printf("Character '%c' not found\n", ch);}return 0;
}

输出结果:

Character ‘o’ found at position: 4

  • 字符串分割strtok(str, delimiters)。将字符串 str 按照分隔符 delimiters 进行分割,并返回分割后的子字符串。
#include <stdio.h>
#include <string.h>int main() {char str[] = "Hello,world,how,are,you";const char delimiters[] = ",";char* token = strtok(str, delimiters);while (token != NULL) {printf("%s\n", token);token = strtok(NULL, delimiters);}return 0;
}

输出结果:

Hello
world
how
are
you

c++语言中string介绍

C++ 中有一个名为 std::string 的标准库类,它提供了处理字符串的功能。要使用 std::string 类,需要包含头文件 <string>

  • 创建字符串对象:可以使用以下方式创建 std::string 对象。
std::string str1;                     // 创建一个空字符串
std::string str2 = "Hello, world!";   // 创建并初始化一个字符串
std::string str3("Welcome");          // 使用字符串字面值创建字符串
  • 字符串拼接:可以使用 + 运算符或者 append() 函数进行字符串拼接。
std::string result = str1 + str2;
#include <iostream>
#include <string>int main() {std::string str1 = "Hello";std::string str2 = " world!";std::string result = str1 + str2;std::cout << "Concatenated string: " << result << std::endl;return 0;
}

结果输出:

Concatenated string: Hello world!

#include <iostream>
#include <string>int main() {std::string str1 = "Hello";std::string str2 = " world!";str1.append(str2);std::cout << "Concatenated string: " << str1 << std::endl;return 0;
}

结果输出:

Concatenated string: Hello world!

  • 字符串长度:可以使用length()size() 成员函数获取字符串的长度。
int length = str.length();
#include <iostream>
#include <string>int main() {std::string str = "Hello, world!";int length = str.length();std::cout << "Length of the string: " << length << std::endl;return 0;
}

输出结果:

Length of the string: 13

#include <iostream>
#include <string>int main() {std::string str = "Hello, world!";int length = str.size();std::cout << "Length of the string: " << length << std::endl;return 0;
}

输出结果:

Length of the string: 13

  • 访问单个字符:可以使用 [] 运算符at() 成员函数来访问字符串中的单个字符。
char ch = str[0];      // 访问第一个字符
char ch2 = str.at(2);  // 访问第三个字符
#include <iostream>
#include <string>int main() {std::string str = "Hello, world!";char firstChar = str[0];char lastChar = str[str.length() - 1];std::cout << "First character: " << firstChar << std::endl;std::cout << "Last character: " << lastChar << std::endl;return 0;
}

输出结果:

First character: H
Last character: !

  • 字符串比较:可以使用 ==、!=、<、>、<=、>= 运算符或者 compare() 函数对字符串进行比较。
if (str1 == str2) {// 字符串相等
}
#include <iostream>
#include <string>int main() {std::string str1 = "Hello";std::string str2 = "World";int result = str1.compare(str2);if (result == 0) {std::cout << "Strings are equal." << std::endl;} else if (result < 0) {std::cout << "str1 is less than str2." << std::endl;} else {std::cout << "str1 is greater than str2." << std::endl;}return 0;
}

输出结果:

str1 is less than str2.

  • 查找子字符串:可以使用 find() 成员函数或者 find_first_of() 函数在字符串中查找子字符串的位置。
size_t pos = str.find("world");   // 查找 "world" 的位置
#include <iostream>
#include <string>int main() {std::string str = "Hello, world!";std::string subStr = "world";size_t position = str.find(subStr);if (position != std::string::npos) {std::cout << "Substring found at position: " << position << std::endl;} else {std::cout << "Substring not found." << std::endl;}return 0;
}

输出结果:

Substring found at position: 7
在上述示例中,我们声明了一个 std::string 类型的字符串变量 str,赋值为 “Hello, world!”。然后,声明了一个 std::string 类型的子字符串变量 subStr,赋值为 “world”。接下来,使用 find() 函数在字符串 str 中查找子字符串 subStr 的位置。如果找到了子字符串,find() 函数返回子字符串的起始位置;如果未找到子字符串,find() 函数返回 std::string::npos。在示例中,我们判断返回值是否等于 std::string::npos,如果不等于,则输出子字符串的位置。

#include <iostream>
#include <string>int main() {std::string str = "Hello, world!";std::string subStr = "ow";size_t position = str.find_first_of(subStr);if (position != std::string::npos) {std::cout << "Substring found at position: " << position << std::endl;} else {std::cout << "Substring not found." << std::endl;}return 0;
}

输出结果:

Substring found at position: 4
在上述示例中,我们使用 find_first_of() 函数在字符串 str 中查找子字符串 subStr 中的任意一个字符的位置。与 find() 函数不同,find_first_of() 函数返回的是子字符串中任意一个字符在原字符串中的第一个匹配位置。在示例中,我们判断返回值是否等于 std::string::npos,如果不等于,则输出子字符串中任意一个字符的位置。

  • 子字符串提取:可以使用 substr() 成员函数提取字符串的子串。
std::string sub = str.substr(7, 5);   // 提取从位置 7 开始的 5 个字符
  • 字符串插入、删除和替换:可以使用 insert()erase()replace() 成员函数对字符串进行插入、删除和替换操作。
http://www.lryc.cn/news/317886.html

相关文章:

  • HarmonyOS NEXT应用开发之深色模式适配
  • Go微服务: 基于Go Micro框架实现微服务调用
  • 大模型prompt提示词如何调优?
  • 【Python/crawl】如何使用Python爬虫将一系列网页上的同类图片下载到本地
  • Postgresql 连接数查看,死锁问题解决
  • ssm蛋糕甜品商城系统(程序+文档+数据库)
  • 算法空间复杂度计算
  • C++ lambda函数个人理解
  • SwiftUI的context Menu
  • 【数据结构】树与堆 (向上/下调整算法和复杂度的分析、堆排序以及topk问题)
  • 安装CDH平台的服务器磁盘满了,磁盘清理过程记录
  • 《互联网的世界》第七讲-能源
  • 前端代码整洁与规范之CSS篇
  • 在【IntelliJ IDEA】中配置【Tomcat】【2023版】【中文】【图文详解】
  • 【SSM】任务列表案例 基本CRUD SSM整合
  • 基于微信小程序的校园跑腿小程序,附源码
  • 网络学习:9个计算机的“网络层”知识点
  • web项目的搭建
  • C++for语句
  • 最新基于R语言lavaan结构方程模型(SEM)技术
  • 【网络安全】-数字证书
  • 【C++ 】stack 和 queue
  • html--彩虹马
  • 如何将应用一键部署至多个环境?丨Walrus教程
  • Redis的一些问题,解决并发的
  • 郭炜老师mooc第十一章数据分析和展示(numpy,pandas, matplotlib)
  • Redis主从架构和管道Lua(一)
  • GTH手册学习注解
  • html5cssjs代码 002 50以内的加法算式
  • [React 进阶系列] React Context 案例学习:使用 TS 及 HOC 封装 Context