Archived

This topic is now archived and is closed to further replies.

jumpjumpjump

What function does this in C++?

Recommended Posts

Since i am moving from blitz to C++ what function is equivilent to mid$ in it? Mid$ does this(from manual): Use this command to grab a set of characters from within a string. You can choose WHERE to start in the string, and how many characters to pick. You''ll probably use this to ''decode'' or ''walk through'' a string to get each character out of it for conversion or validation. See the Example. What do i need to do to do that in C++? Sharp Basic baby!
My Homepage | My Forums Please Register | Google | Sharp Basic - Coming summer 2004! Wanna Beta test? Clicky!

Share this post


Link to post
Share on other sites
Actually, the substr() method of std::string isn''t static, so.. like this:

#include <string>
#include <iostream>

using std::string;
using std::cout;

int main(int argc, char* argv[])
{
string s("This is my string.");
cout << "Part of the string: " << s.substr(5,5) << "\n";
return 1;
}

Program output:

Part of the string: is my

s.substr(0,4) would return "this". The first argument is the string position to start extraction, the second argument is how many characters to extract.

brett

Share this post


Link to post
Share on other sites
quote:
Original post by aftli
s.substr(0,4) would return "this".

Or rather, to not confuse things, "This".

Share this post


Link to post
Share on other sites
You could always make a function to do it yourself:

In this function, str2 is the one that gets modified
newstrlen is the length you want the new string to be and
lenstr2 is the lenght of the string that is going to be modified

and by modified, i mean, the result will be put in lenstr2.

void mid(char * str1, char * str2, int newstrlen, int lenstr2, int start)
{
int i = 0;

//Copy characters into new string
while (i {
str2 = str1[i + (start-1)];
i++;
}
str2[newstrlen] = ''\0'';

}

Share this post


Link to post
Share on other sites
quote:
Original post by jumpjumpjump
You''ll probably use this to ''decode'' or ''walk through'' a string to get each character out of it for conversion or validation.
In that case (in the case where you only want one character at a time) use operator[], will be quite a bit faster than using substr.

Share this post


Link to post
Share on other sites
quote:
Original post by squicklid
hmm, for some reason that didn't post properly.

the condition in the while statement is supposed to be:

while (i


HTML is eating your <. Use &lt; (and &amp; )

while (i<newstrlen && i<lenstr2)

[edited by - Doc on April 4, 2004 9:28:40 PM]

Share this post


Link to post
Share on other sites
When posting code, use the "code" tag (enclose code in square brackets). If you don''t, your code might be interpreted wrongly as html from a < onwards.

Share this post


Link to post
Share on other sites
quote:
Original post by paulsdsrubbish
When posting code, use the "code" tag (enclose code in square brackets). If you don''t, your code might be interpreted wrongly as html from a < onwards.


That would be the [source][/source] tags, not [code][/code] tags, which do not disable HTML parsing, as many a poster has discovered when trying to use i as an array index (foo[i] -> oops, why is everything in italics).

Share this post


Link to post
Share on other sites