开发者

C++ How to copy text in string (from i.e. 8 letter to 12 letter)

开发者 https://www.devze.com 2022-12-27 02:45 出处:网络
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,

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


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.


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);


Check out the sections on copying and substrings:

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


Take a look at the answers to this question.



// For fun...  Assuming 8 and 17 are index values, could be off by 1
// Treat s and a char[], C style.

char *nextStringPtr = nextString; for(int i=8; i<17; i++) { *nextStringPtr++ = s[i]; } *nextStringPtr = 0;

0

精彩评论

暂无评论...
验证码 换一张
取 消