?
Solved

taking substrings of a string

Posted on 2013-05-18
3
Medium Priority
?
255 Views
Last Modified: 2013-05-18
Why this little code doesn't work?I want to take a string and then assign substrings to another string.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string x = "ALERT";

string y = x[0] + x[1];   
cout << y;    //PRINT AL
return 0;
}

Open in new window

0
Comment
Question by:codeBuilder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 39177182
0
 
LVL 32

Accepted Solution

by:
phoffric earned 1000 total points
ID: 39177188
x[0] returns char, and + on char doesn't mean concatenate. It means add the two char numbers together.
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 1000 total points
ID: 39177233
Since phoffric has elaborated why your attempt does not work, here's what would work - and it's surprisingly close to your question title:

#include <iostream>
#include <string>
using namespace std;
int main()
{
string x = "ALERT";

string y = x.substr(0,2);   // 1st parameter is start position, 2nd is length
cout << y;    //PRINT AL
return 0;
}
                                  

Open in new window


See also http://en.cppreference.com/w/cpp/string/basic_string/substr and http://en.cppreference.com/w/cpp/string/basic_string
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question