You can access the characters in a string by referring to its index number inside square brackets []
.
This example prints the first character in myString:
Note: String indexes start with 0: [0] is the first character. [1] is the second character, etc.
This example prints the second character in myString:
To change the value of a specific character in a string, refer to the index number, and use single quotes:
string myString = "Hello";
myString[0] = 'J';
cout << myString;
// Outputs Jello instead of Hello
Try it Yourself »
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!