Sorry for what maybe will be an easy question to answer, but here we go.
itemname2 = tsection.htmlText.Data;
int rf = itemname2.rfind("'>");
itemname2 = itemname2.replace(0, rf + 2, "");
WriteLogFile(itemname2);
The code above works perfectly if I was to be passing a string into itemname2, however
tsection.htmlText.Data = wchar_t *TArray::Data
You can turn itemname2 into a std::wstring and then do it very similar way:
int rf = itemname2.rfind(L"'>");
itemname2 = itemname2.replace(0, rf + (2*sizeof(wchar_t)), "");
You need to use L prefix to let compiler interpret lietral string as a wide one.
Related
The row string is Chinese,e.g. "九张".And the regular expression is "%[^张]";
Code segment:(mingw-32 ,windows10)
char szTemp[64] = {0};
sscanf("九张","%[^张]",szTemp);
printf("szTemp = [%s]\n",szTemp);
szTemp = [綸
When I print szTemp's value ,it was messed up codes;
But,when I replace "九" to "一", it work.
char szTemp[64] = {0};
sscanf("九张","%[^张]",szTemp);
printf("szTemp = [%s]\n",szTemp);
szTemp = [一]
My development environment is Fedora21.
Thanks
I want to do something similar to this:
char* a = (char*)msg[0];
char* b = (char*)msg[1];
char* c = a + "," + b;
Where msg is an array of int.
N.B.: This is Arduino C++, not regular C++.
Arduino doesn't use std::string, instead it uses String (note the capital S and dropped std::). They're used the same way as std::string for the most part. So basically you should just be able to do this:
String a("hello");
String b(" world");
c = a + b;
If you want to convert an integer to a String, it has a constructor to do just that, e.g.:
String a = String(msg[0]);
String b = String(msg[1]);
You can find more examples here and here.
See strcat.
You seem to be programming C, not C++.
This should be covered in the most basic tutorials.
SOLUTION
so here is my solution thank everyone.
String a = String(msg[0]);
String b = String(msg[1]);
String c = a + "," + b;
char* d;
c.toCharArray(d,c.length());
mclient.publish("topic1/sensorAck",d);
I want to get int from cstring.
Here is Code.
CStringArray paramArray;
paramArray.Add((LPCSTR)"5");
paramArray.Add((LPCTSTR)"151");
pvarArguments = new CComVariant[2];
pvarArguments[0] = (LPCTSTR)paramArray[1];
CString str;
str = (CStringA)pvarArguments[0];
int nlen = _wtoi(str.GetBuffer());
When I run my program, I always get value 0, and I can't understand why it is.
Please help me.
From TFM (emphasis mine):
Each function returns the int value produced by interpreting the input
characters as a number. The return value is 0 for atoi and _wtoi, if
the input cannot be converted to a value of that type.
Print the string or examine it using a debugger. There may be invalid (including unprintable) characters in the string.
It's hard to tell even what you are trying to do. You do know that C++ arrays are 0-based, right? I ask because this line of code:
pvarArguments[0] = (LPCTSTR)paramArray[1];
is totally messed up. I don't understand why it's not throwing an exception when trying to index an element in a CStringArray that is equal to the count of elements. You can only index to count-1 ==>> which in this case is "0".
Your pvarArguments[0] will have junk in it--I have no idea why an exception wasn't thrown.
If you want to get a different type out of a variant, you can use VariantChangeType() and not mess with wcstoi or atoi. It will give an error code if it fails instead of just returning 0. If you are hell bent on using wcstoi or atoi on a CString, use _tstoi() which works whether you nave UNICODE defined or not.
CStringA implies ANSI string type which would require atoi not _wtoi.
I suggest:
CString str(pvarArguments[0]);
int nlen = atoi(str.GetString());
GetBuffer() is not ideal because you must remember to later ReleaseBuffer().
EDIT: In light of the new information, try this:
paramArray.Add(L"5");
paramArray.Add(L"151");
The L macro makes the string wchar_t aware. If L doesn't work try _T instead. And then use _wtoi or _tstoi.
CStringArray paramArray;
paramArray.Add(_T("5"));
paramArray.Add(_T("151"));
CComVariant *pvarArguments = new CComVariant[2];
pvarArguments[0] = (LPCTSTR)paramArray[1];
CString str;
str = pvarArguments[0].bstrVal;
int nlen = _ttoi(LPCTSTR(str));
per suggestion above, this compiles for me:
rs->GetFieldValueString(0).Left(1) == sBoroCode
&& (_ttoi(LPCTSTR(sLowHouseNo)) % 2) == (_ttoi(LPCTSTR(rs->GetFieldValueString(2))) % 2)
I have to split a string into several ones. In fact what i need is to parse some input from a file that is in the following format (i9, i9, i2) for example.
i9 means a decimal number as:
(5blankspaces)4567
So i need to retrieve that numbers properly. The width is always fixed so every number must obey that.
A correct instance of that format would be
(5spaces)4567(6spaces)453(1space)2
or
(5spaces)4567(6spaces)45322 (where 22 is the argument for i2 in this case)
The white spaces before the numbers are giving me headache, so i thought i could split every argument into a character array and then convert it to integer since the %d specifier ignores all blank space and i dont know how to use the width as well as ignoring spaces.. (if this can be done, i mean, parsing all to integer please say so!!)
If not.. well, i would need help to parse every string into substring, so far i've done this but it's not working:
while (fgets(buffer, 600, f)) {
sscanf(buffer, "%9[^\n]s %9[^\n]s %2[^\n]s", arg1, arg2, arg3);
....
}
Please, any help would be greatly appreciated!!!
This answer is C. That is why I used the variable name new.
Use strncpy() and terminate the result properly.
char part1[10], part2[10], part3[3];
const char *new = " 4567 45322\n"; /* the line read */
strncpy(part1, new, 9); part1[9] = 0;
strncpy(part2, new+9, 9); part2[9] = 0;
strncpy(part3, new+18, 2); part3[2] = 0;
I suggest you do not try to write multi-language source files.
In C++, use substr(), along with the usual string to integer conversions:
#include <string>
std::string s = " 1234 78922";
std::string s1 = s.substr(0, 9);
std::string s2 = s.substr(9, 9);
std::string s3 = s.substr(18); // or substr(18, 2)
int n1 = std::stoi(s1), n2 = std::stoi(s2), n3 = std::stoi(s3);
Apply the usual length checks where appropriate to validate that the input is indeed in the correct format.
i have code like this
string xml_path(conf("CONFIG"));
xml_path+=FILE_NAME;
Where,
conf function returns char * and FILE name is const char *
I want to combine it to one line like
xml_path(conf("CONFIG")).append(FILE_NAME)
how do i do it?
any suggestions ??
Question asked for one line:
string xml_path = string(conf("CONFIG")) + string(FILE_NAME);
(I assume xml_path is the name of the variable, and not some sort of call in a library I don't know about).
Alternatively, if you want to format variable of different type, use a ostringstream.
eg.
std::ostringstream oss;
int a = 2;
char *s = "sometext";
oss<<s<<a<<endl;
cout<<oss.str(); // will output "sometext2"
const char * f = "foo";
char * b = "bar";
string s = string( f ) + b;
Note that you cannot use append(-0 because neither of the strings invvolved is a std:;string. If you really want to append, it will have to be two-stage process:
string s ( f );
s.append( b );
string xml_path(conf("CONFIG"));
xml_path += string(FILE_NAME);
should do the trick.