Note: This code must be used altogether with the RTRIM function.
wstring PARSE(IN wstring sMain, IN wstring sDelim, IN long nIndex) {
wstring sResult = L"";
long nLength = sDelim.length();
if (nLength == 0) { sDelim = L","; ++nLength; } // Use comma "," as default delimiter
sMain = RTRIM(sMain, sDelim); sMain += sDelim;
if (sMain.length() && nLength) {
long prev_pos = 0, pos = 0, nCount = 0;
while( (pos = sMain.find(sDelim, pos)) != std::wstring::npos ) {
wstring substring(sMain.substr(prev_pos, pos - prev_pos));
++nCount;
if (nCount == nIndex) { sResult = substring; break; }
prev_pos = ++pos;
}
}
return sResult;
}
James,
Yes, i do not care much of 32-bit timing, my first concern being to convert my PowerBASIC code to C/C++, with minimum changes, in order to maintain both code in parallel.
This is the reason why i need to have first all the basic string manipulation facilities we are accustomed in PB, keeping the same name (and syntax when possible) to ease readability.