C++ 字符串

C++ 字符串 首页 / C++入门教程 / C++ 字符串

在C++中,字符串是 std::string 类的对象,代表字符序列。 我们可以对字符串执行许多操作,例如连接,比较,转换等。

让我们看一下C++字符串的简单示例。

#include <iostream>
using namespace std;
int main( ) {
    string s1 = "Hello";  
        char ch[] = { 'C', '+', '+'};  
        string s2 = string(ch);  
        cout<<s1<<endl;  
        cout<<s2<<endl;  
}

输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-strings.html

来源:LearnFk无涯教程网

Hello
C++

字符串比较-strcmp 

让我们看一下使用strcmp()函数进行字符串比较的简单示例。

#include 
#include <cstring>
using namespace std;
int main ()
{
  char key[] = "mango";
  char buffer[50];
  do {
     cout<<"What is my favourite fruit? ";
     cin>>buffer;
  } while (strcmp (key,buffer) != 0);
 cout<<"Answer is correct!!"<<endl;
  return 0;
}

输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-strings.html

来源:LearnFk无涯教程网

What is my favourite fruit? apple
What is my favourite fruit? banana
What is my favourite fruit? mango
Answer is correct!!

字符串连接-strcat

让我们看一下使用strcat()函数进行字符串连接的简单示例。

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    char key[25], buffer[25];
    cout << "Enter the key string: ";
    cin.getline(key, 25);
    cout << "Enter the buffer string: ";
     cin.getline(buffer, 25);
    strcat(key, buffer); 
    cout << "Key = " << key << endl;
    cout << "Buffer = " << buffer<<endl;
    return 0;
}

输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-strings.html

来源:LearnFk无涯教程网

Enter the key string: Welcome to
Enter the buffer string:  C++ Programming.
Key = Welcome to C++ Programming.
Buffer =  C++ Programming.

字符串复制-strcpy

让我们看一下使用strcpy()函数复制字符串的简单示例。

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    char key[25], buffer[25];
    cout << "Enter the key string: ";
    cin.getline(key, 25);
    strcpy(buffer, key);
    cout << "Key = "<< key << endl;
    cout << "Buffer = "<< buffer<<endl;
    return 0;
}

输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-strings.html

来源:LearnFk无涯教程网

Enter the key string: C++ Tutorial
Key = C++ Tutorial
Buffer = C++ Tutorial

字符串长度-strlen

让我们看一下使用strlen()函数查找字符串长度的简单示例。

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    char ary[] = "Welcome to C++ Programming";
    cout << "Length of String = " << strlen(ary)<<endl;
    return 0;
}

输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-strings.html

来源:LearnFk无涯教程网

Length of String = 26

字符串函数列表

FunctionDescription
int compare(const string& str)它用于比较两个字符串对象。
int length()它用于查找字符串的长度。
void swap(string& str)它用于交换两个字符串对象的值。
string substr(int pos,int n)它创建一个新的n个字符的字符串对象。
int size()它以字节为单位返回字符串的长度。
void resize(int n)它用于将字符串的长度调整为最多n个字符。
string& replace(int pos,int len,string& str)它替换了从字符位置pos开始并跨越len个字符的字符串部分。
string& append(const string& str)它将在另一个字符串对象的末尾添加新字符。
char& at(int pos)它用于访问指定位置pos处的单个字符。
int find(string& str,int pos,int n)用于查找参数中指定的字符串。
int find_first_of(string& str,int pos,int n)它用于查找指定序列的首次出现。
int find_first_not_of(string& str,int pos,int n )它用于在字符串中搜索与该字符串中指定的任何字符都不匹配的第一个字符。
int find_last_of(string& str,int pos,int n)用于在字符串中搜索指定序列的最后一个字符。
int find_last_not_of(string& str,int pos)它搜索与指定序列不匹配的最后一个字符。
string& insert()它将在位置pos指示的字符之前插入一个新字符。
int max_size()它找到字符串的最大长度。
void push_back(char ch)它在字符串的末尾添加了一个新字符ch。
void pop_back()它删除字符串的最后一个字符。
string& assign()它将新值分配给字符串。
int copy(string& str)它将字符串的内容复制到另一个。
char& back()它返回最后一个字符的引用。
Iterator begin()它返回第一个字符的引用。
int capacity()它返回为字符串分配的空间。
const_iterator cbegin()它指向字符串的第一个元素。
const_iterator cend()它指向字符串的最后一个元素。
void clear()它从字符串中删除所有元素。
const_reverse_iterator crbegin()它指向字符串的最后一个字符。
const_char* data()它将字符串的字符复制到数组中。
bool empty()它检查字符串是否为空。
string& erase()它删除指定的字符。
char& front()它返回第一个字符的引用。
string&  operator+=()它在字符串的末尾附加一个新字符。
string& operator=()它为字符串分配一个新值。
char operator[](pos)它在指定位置pos检索一个字符。
int rfind()它搜索字符串的最后一次出现。
iterator end()它引用字符串的最后一个字符。
reverse_iterator rend()它指向字符串的第一个字符。
void shrink_to_fit()它减少了容量并使它等于字符串的大小。
char* c_str()它返回指向包含空终止字符序列的数组的指针。
const_reverse_iterator crend()它引用字符串的第一个字符。
reverse_iterator rbegin()它引用字符串的最后一个字符。
void reserve(inr len)它要求更改容量。
allocator_type get_allocator();它返回与字符串关联的已分配对象。

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

深入拆解Tomcat & Jetty -〔李号双〕

职场求生攻略 -〔臧萌〕

大厂晋升指南 -〔李运华〕

手把手教你玩音乐 -〔邓柯〕

eBPF核心技术与实战 -〔倪朋飞〕

遗留系统现代化实战 -〔姚琪琳〕

手把手教你落地DDD -〔钟敬〕

Python实战 · 从0到1搭建直播视频平台 -〔Barry〕

云时代的JVM原理与实战 -〔康杨〕

好记忆不如烂笔头。留下您的足迹吧 :)