---
title: "std::string的基本用法"
author: "mmdev"
date: "2004-07-06T00:01:00Z"
tags: []
slug: "std-string"
draft: false
lastmod: "2004-07-06T00:01:00Z"
id: 1006741
url: "https://mmdev.iteye.com/blog/1006741"
---

可以直接相加：

> string s1 = "Hello";  
> string s2 = "world";  
> string s3 = s1 + ", " + s2 + "!\\n";

可以附加：

> s1 += '\\n';

可以比较：

> if (s1==s2) {  
> ...  
> }  
> else if(s1=="yes") {  
> ...  
> }

可以操作子串：

> string name = "Niels Stroustrup";  
> string s = name.substr(6, 10); // s = "Stroustrup"  
> name.replace(0, 5, "Nicholas"); // name becomes "Nicholas Stroustrup"

转换为C-Style String：

> printf("name: %s", name.c\_str());