tags:

views:

149

answers:

5

Hello

This is not homework, I need this for my program :)

I ask this question, because I searched for this in Google about 1 hour, and I don't find anything ready to run. I know that is trivial question, but if you will help me, you will make my day :)

Question:

How to copy text in string (from for example 8 letter to 12 letter) and send to other string?

I have string:

string s = "RunnersAreTheBestLovers";

and I want text from 8 letter to 17 letter in next string

Alice90

+4  A: 

I assume you're trying to get the 8th - 17th characters in a another string. If so you should use the substring method string::substr

string s = "RunnersAreTheBestLovers";
string other = s.substr(8, 9);
Ben S
Thanks, I saw this function, but I though, that it accept only 1 argument :)
Alice90
If you only use one argument, it will take the string from that character to the end.
Ben S
Note that the second argument to `substr()` is the *number* of characters to extract, not the *ending index* (this is different from some other languages such as Java).
Greg Hewgill
Thanks for pointing that out, fixed my code example.
Ben S
+6  A: 

The string class has a substr method:

string t = s.substr(8, 9);

The first parameter is the starting index and the second parameter is the number of characters to extract.

Greg Hewgill
A: 

Check out the sections on copying and substrings:

http://www.cprogramming.com/tutorial/string.html

John at CashCommons
A: 

Take a look at the answers to this question.

typoknig
A: 
Michael Dorgan