What are some of the functions (with syntax) for manipulating char variables? Like in VB there's trim() and mid() and left() and all those great functions... what would i do in c++ to accomplish these types of functions?
Printable View
What are some of the functions (with syntax) for manipulating char variables? Like in VB there's trim() and mid() and left() and all those great functions... what would i do in c++ to accomplish these types of functions?
don't you mean char*?
at the moment, for pure operations on char arrays, then string.h has some useful things, all beginning with str.
If you want ease of use, try the STL string class:
there's lots of member functions to do what you need - the documentation's pretty good.Code:#include <iostream>
#include <string>
using namespace std;
void main() {
string sMyStr = "A string";
cout << sMyStr << endl;
}
#include <string.h>
// example of using some string functions
class Cmessage {
char* pmessage //pointer to object text string
public:
// function to display message
void showit();
{
cout << endl << pmessage;
}
// constructor defination
Cmessage(const char* text = "default message")
{
pmessage = new char[strlen(text) + 1]; // allocate space
// for text
strcpy(pmessage,text); // copy text to new memory
~Cmessage(); //destructor protype
};
// deconstructor to free up memory created by new
Cmessage::~Cmessage()
{
cout << "destructor called"; // just to track what happens
<< endl;
delete[] pmessage; // free memory assigned to pointer
}
"A man ought to read as inclination leads him for what he reads as a task will do him little good"