How to replace a '%' character in CComBSTR with "%%"
-
Hi I just wanted to replace a '%' character in CComBSTR with "%%". But the code does not support CString. The solution should be UNICODE support. Any methods to perform this operation ? Thanks In Advance. Babu
Today is a gift, that's why it is called the present.
-
Hi I just wanted to replace a '%' character in CComBSTR with "%%". But the code does not support CString. The solution should be UNICODE support. Any methods to perform this operation ? Thanks In Advance. Babu
Today is a gift, that's why it is called the present.
CComBstr class doesn't have a method to replace a sub-string with another or to insert another string in specified position. All you can do is, get BSTR which is wrapped in CComBSTR class ( either by m_str member or by operator BSTR). You need to copy this to another CComBSTR. You may use its Append() method or overloaded += operators for ease.
CComBSTR str("here%are%percentages%");
CComBSTR newstr;WCHAR split[] = L"%";
WCHAR* pos = wcstok(str.m_str, split);
while(0 != pos) {
newstr.Append(pos);
newstr.Append(L"%%");pos = wcstok(NULL, split);
}NOTE: I would like to remind that BSTR data type has preceding header components, and the final string is terminated with double null characters. This is how system allocates memory for BSTR. There can be embedded single null characters in BSTR string data. In that case, wcslen() and wcstok() functions won't work as expected. Use CComBSTR::Length() or SysStringLen() to get length of string. You may need to examine each character in string till you find two continuous '\0' characters. During this, whenever you find a '%' add an additional '%' more tho the new string. :|
Unicode support is completely ensured. :)modified on Thursday, March 24, 2011 9:09 AM
-
CComBstr class doesn't have a method to replace a sub-string with another or to insert another string in specified position. All you can do is, get BSTR which is wrapped in CComBSTR class ( either by m_str member or by operator BSTR). You need to copy this to another CComBSTR. You may use its Append() method or overloaded += operators for ease.
CComBSTR str("here%are%percentages%");
CComBSTR newstr;WCHAR split[] = L"%";
WCHAR* pos = wcstok(str.m_str, split);
while(0 != pos) {
newstr.Append(pos);
newstr.Append(L"%%");pos = wcstok(NULL, split);
}NOTE: I would like to remind that BSTR data type has preceding header components, and the final string is terminated with double null characters. This is how system allocates memory for BSTR. There can be embedded single null characters in BSTR string data. In that case, wcslen() and wcstok() functions won't work as expected. Use CComBSTR::Length() or SysStringLen() to get length of string. You may need to examine each character in string till you find two continuous '\0' characters. During this, whenever you find a '%' add an additional '%' more tho the new string. :|
Unicode support is completely ensured. :)modified on Thursday, March 24, 2011 9:09 AM
-
or ...
CComBSTR str("here%are%percentages%");
CString workingString(str);
workingString.Replace(_T("%"),_T("%%"));
str=workingString; -
Its ok. But I guess you didn't notice "But the code does not support CString" in the actual question :)