c++中定义了string类字符数组,如何输出string里单个字符(比如第二个字符)。

如题所述

直接取就可以了。例如:
string name="hello";
name[0];//这里name[0]就是第一个字符'h'
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-03-12
d
Press any key to continue

#include <iostream>
#include <string>
using namespace std;
main()
{
string aa="123asdf";
cout<<aa[5]<<endl;
}
第2个回答  2012-03-12
/* STRCPY.C: This program uses strcpy
* and strcat to build a phrase.
*/

#include <string.h>
#include <stdio.h>

void main( void )
{
char string[80];
strcpy( string, "Hello world from " );
strcat( string, "strcpy " );
strcat( string, "and " );
strcat( string, "strcat!" );
printf( "String = %s\n", string );
}

Output
String = Hello world from strcpy and strcat!
相似回答