How can I echo characters before and after a string?

How can I call or get a specifc characters in a string in C++?

  • I'm trying to get certain parts of a string and assign it to another string variable. string intro = "Hai"; string firstLetter; string theRest; firstLetter = first letter of intro; theRest = last two letters of intro;

  • Answer:

    string firstLetter = intro.substr(0, 1); string theRest = intro.substr(intro.length() - 2);

zNelson2... at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

look at using the substr() function

Patrick

cramshark is almost right. string firstLetter = intro.substr(0,1); string theRest = intro.substr(2); The first argument to subtstr is the index of the first character in the result. The second argument is the maximum length of the result. If there's no second argument, or if it's specified as string::npos, or if it's greater than the number of characters in the rest of the string, then the rest of the string is returned. Find a reference, online or in a book, and look over the members in any class you're using. That's how you find this stuff out. For an online reference, I like http://www.cplusplus.com/reference/

husoski

I don't know very much C++, but this is the general function for that kind of call: string substr ( size_t pos = 0, size_t n = npos ) const; I assume that that is starting and ending point.

Thomas

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.