How to trim a string in a _variant_t 
Author Message
 How to trim a string in a _variant_t

From the following expression

recordset->Item[1]->Value

I recieve a _variant_t. This contains a string which I need to convert to a
double. But of cause, if the string contains a space-character for example,
the conversion from string to double will fail (type missmatch).

I'm using the (double) operator in the _variant_t wrapper to do the
conversion. So before I do this conversion, I need to remove any spaces
found in the string. How do I do this? The pseudo-code would look like:

double dblMyVar = (double) (TRIM(recordset->Item[1]->Value)

This would be a simple procedure in C using a standard string, but all these
COM/ATL datatypes/wrappers and Unicode stuff makes me confused.

// jP



Tue, 25 Mar 2003 03:00:00 GMT  
 How to trim a string in a _variant_t

Use VarR8FromStr or use VariantChangeType to change the variant with string
to variant with double then get the thing..
I assume _variant_t already has cast operators, so you might be able to do
just (double) (Item[0]->Value)..

--
Girish Bharadwaj

Quote:
> From the following expression

> recordset->Item[1]->Value

> I recieve a _variant_t. This contains a string which I need to convert to
a
> double. But of cause, if the string contains a space-character for
example,
> the conversion from string to double will fail (type missmatch).

> I'm using the (double) operator in the _variant_t wrapper to do the
> conversion. So before I do this conversion, I need to remove any spaces
> found in the string. How do I do this? The pseudo-code would look like:

> double dblMyVar = (double) (TRIM(recordset->Item[1]->Value)

> This would be a simple procedure in C using a standard string, but all
these
> COM/ATL datatypes/wrappers and Unicode stuff makes me confused.

> // jP



Tue, 25 Mar 2003 03:00:00 GMT  
 How to trim a string in a _variant_t

_variant_t vnt = recordset->Item[1]->Value;
<ytour favorite string wrapper> str = V_BSTR(&vnt);
<trim, convert etc using your favorite class>

or

_variant_t vnt = recordset->Item[1]->Value;
wcscpy(wszBuffer, V_BSTR(&vnt));
<manipulate using the C RTL wcsXXX functions>

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD

MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================


Quote:
> From the following expression

> recordset->Item[1]->Value

> I recieve a _variant_t. This contains a string which I need to convert to
a
> double. But of cause, if the string contains a space-character for
example,
> the conversion from string to double will fail (type missmatch).

> I'm using the (double) operator in the _variant_t wrapper to do the
> conversion. So before I do this conversion, I need to remove any spaces
> found in the string. How do I do this? The pseudo-code would look like:

> double dblMyVar = (double) (TRIM(recordset->Item[1]->Value)

> This would be a simple procedure in C using a standard string, but all
these
> COM/ATL datatypes/wrappers and Unicode stuff makes me confused.

> // jP



Tue, 25 Mar 2003 03:00:00 GMT  
 How to trim a string in a _variant_t

The problem with the (double) operator of the _variant_t wrapper is that it
throws an exception if it finds any non-supported numeric character, like a
space in the string.

// jP

Quote:

> Use VarR8FromStr or use VariantChangeType to change the variant with string
> to variant with double then get the thing..
> I assume _variant_t already has cast operators, so you might be able to do
> just (double) (Item[0]->Value)..

> --
> Girish Bharadwaj


> > From the following expression

> > recordset->Item[1]->Value

> > I recieve a _variant_t. This contains a string which I need to convert to
> a
> > double. But of cause, if the string contains a space-character for
> example,
> > the conversion from string to double will fail (type missmatch).

> > I'm using the (double) operator in the _variant_t wrapper to do the
> > conversion. So before I do this conversion, I need to remove any spaces
> > found in the string. How do I do this? The pseudo-code would look like:

> > double dblMyVar = (double) (TRIM(recordset->Item[1]->Value)

> > This would be a simple procedure in C using a standard string, but all
> these
> > COM/ATL datatypes/wrappers and Unicode stuff makes me confused.

> > // jP



Tue, 25 Mar 2003 03:00:00 GMT  
 How to trim a string in a _variant_t


Quote:
> _variant_t vnt = recordset->Item[1]->Value;
> <ytour favorite string wrapper> str = V_BSTR(&vnt);
> <trim, convert etc using your favorite class>

> or

> _variant_t vnt = recordset->Item[1]->Value;
> wcscpy(wszBuffer, V_BSTR(&vnt));
> <manipulate using the C RTL wcsXXX functions>

But how do you manipulate a wstring (wchar_t?) string character by
character? When using a standard single-byte char string in C++ my RTrim()
function would look something like:

// Removes ending spaces in string
void RTrim(char *lpzString)
{
    int iOffset = strlen(lpzString);

    // Scan for the first non-space character in string from the end and
backwards
    for(; *(lpzString + iOffset) != ' ' || iOffset > 0; iOffset--);
    // Null terminate string
    *( lpzString + iOffset + 1) = NULL;

Quote:
}

You get the point..

How would you do the same with a wstring (or wchar_t, multibyte-strings)?
_bstr_t strings don't use NULL characters and in Unicode-strings every
characters occupies two bytes and so on..

// jP



Sat, 29 Mar 2003 03:00:00 GMT  
 How to trim a string in a _variant_t

Instead of stlen use wcslen. Instead of ' ' use L' ', etc. As for using STL,
you don't have to - use my second recommendation...

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD

MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================


Quote:



> > _variant_t vnt = recordset->Item[1]->Value;
> > <ytour favorite string wrapper> str = V_BSTR(&vnt);
> > <trim, convert etc using your favorite class>

> > or

> > _variant_t vnt = recordset->Item[1]->Value;
> > wcscpy(wszBuffer, V_BSTR(&vnt));
> > <manipulate using the C RTL wcsXXX functions>

> But how do you manipulate a wstring (wchar_t?) string character by
> character? When using a standard single-byte char string in C++ my RTrim()
> function would look something like:

> // Removes ending spaces in string
> void RTrim(char *lpzString)
> {
>     int iOffset = strlen(lpzString);

>     // Scan for the first non-space character in string from the end and
> backwards
>     for(; *(lpzString + iOffset) != ' ' || iOffset > 0; iOffset--);
>     // Null terminate string
>     *( lpzString + iOffset + 1) = NULL;
> }

> You get the point..

> How would you do the same with a wstring (or wchar_t, multibyte-strings)?
> _bstr_t strings don't use NULL characters and in Unicode-strings every
> characters occupies two bytes and so on..

> // jP



Sat, 29 Mar 2003 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Accessing string elements in a _variant_t array

2. Trim a comma off end of string???

3. Help to trim spaces of a string

4. trim the string

5. String.Trim() behavior

6. String.Trim is this correct functionality

7. String.Trim();

8. Need help to trim spaces off a string

9. trimming a string

10. How to trim a String?

11. Right trim string function in C

12. trim for strings

 

 
Powered by phpBB® Forum Software