How to cut off part of a string array

If I wanted to cut off a part of a string array, let's say for example:
1
2
string hello[5];
hello[0] = "Dance"


and I only wanted the first three letters so it would only display "Dan" how would I go about that?
hello is an array of 5 strings. The first string in the array is set to "Dance". If you want to "cut off" everything but the first three characters from that string you can use the resize function.

hello[0].resize(3);
Last edited on
thereĀ“s a method of the string class, substring

check this out:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
using namespace std;

int main(){
string S = "hello";

cout << S << endl;

S = S.substr(0,3);

cout << S << endl;


}
closed account (3qX21hU5)
I'm not positive but wouldn't string::erase work also? Not sure if you want to erase the characters totally or not but it is a option. Also not sure how this works with arrays since I'm not the best with arrays since I tend to use std containers more often.
Topic archived. No new replies allowed.