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
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:
- How Can I Get Started As A Freelance Web Developer?Best solution by code.tutsplus.com
- How can I get objects in a S3 bucket?Best solution by Stack Overflow
- How can I transfer video from a dvr box to a computer?Best solution by Yahoo! Answers
- How can I get an audio alert when I get a new e-mail?Best solution by Yahoo! Answers
- How can I get a job at a resort for a Summer?Best solution by eHow old
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.