C++ String Ptr Question

SalientKing

Member
Jan 28, 2005
144
0
0
SO ive received some old hand me down code that i want to use, but I've avoided C++ for awhile now and have no idea how to do what i want to do

First off i have a union, and i want to pass this union a ptr to a string. But i need to edit this string before i can send it over, and im using the string.h header.

Say the string var contains "stuff"
I want to remove the qoutes
So i was thinking taking var+1 and storing that into another var
so var2 = var+1 = stuff"
But then how to i replace the second " with a end line char?
Then i need to pass a ptr to the newly edited string to the union

Also im a little lost with the whole passing a ptr/memory allocation deal with c++ strings.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
If you're using a string library, you want a substring function, and to pass it a starting position of the second character (index max be 1 or 2) and a length of (string length -2).

if length is 10 and the index is 0-based, something like
if ( var.legnth() > 2 )
var2 = var.substring (1, var.length() -2 ) ;
else
var2 = var ;

I made up those string class function names, you'll need to Read The Friendly Docs to find the real class or library functions.


Or do you mean C character array strings rather than C++ class library strings? Then something like
if (strlen(var1) > 2)
{
strcpy(var2, &var1[ 1 ] ) ;
var2 [ strlen(var2) - 1 ] = 0 ;
}
else
strcpy( var2, var1 ) ;

...this does assume var2 has been allocated a buffer large enough for var1