I know I can have char, but then there is string.h with the string function in it... all I want to know is how to do the equivalents of Mid$(), Left$(), Len(), and Right$() with C++. I've got a really old book and it doesnt really cover the basics.
Printable View
I know I can have char, but then there is string.h with the string function in it... all I want to know is how to do the equivalents of Mid$(), Left$(), Len(), and Right$() with C++. I've got a really old book and it doesnt really cover the basics.
if you use the string class located in string, there is a substr function.
There is also the length function belonging to the string class.Code:#include <string>
using namespace std;
//substr(start, depth)
int main()
{
string str = "hello";
// 0 1 2 3 4
// h e l l o
string s = str.substr(1, 2); //starts at position 1 (e) and goes three spaces.
//s contains ell
return(0);
}
If you are talking about using char* then you can get the length by using strlen() in string.h(cstring is the standard library name). As we all know you can access an individual character in an array of characters by using [ and ] (ex: pcBuf[5] returns the 5th element of the variable). From there you can make your own left, mid, and right functions, with almost no effort.Code:string str = "hello";
int len = str.length(); //len contains 5