Linux 拨号vps windows公众号手机端

c++怎么创建字符串

lewis 8年前 (2017-04-19) 阅读数 9 #程序编程
文章标签 c++

在C++中,可以使用std::string类来创建字符串。下面是一些常见的创建字符串的方法:

  1. 使用字符串字面量创建字符串:
std::string str = "Hello, World!";
  1. 使用std::string构造函数创建字符串:
std::string str("Hello, World!");
  1. 使用+操作符连接字符串:
std::string str1 = "Hello";
std::string str2 = "World";
std::string str = str1 + ", " + str2 + "!";
  1. 使用append()函数添加字符串:
std::string str = "Hello";
str.append(", World!");
  1. 使用insert()函数插入字符串:
std::string str = "Hello";
str.insert(5, ", World!");
  1. 使用substr()函数截取子字符串:
std::string str = "Hello, World!";
std::string subStr = str.substr(7, 5); // 从索引7开始截取长度为5的子字符串

这些只是一些创建字符串的常见方法,实际上还有很多其他操作和函数可以用来创建和操作字符串。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门