SAS/StringUtils.cpp

59 lines
1.4 KiB
C++
Raw Permalink Normal View History

/*
* <EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߽ӿ<EFBFBD>ʵ<EFBFBD><EFBFBD>
*/
#include"StringUtils.h"
string doubleToString(double num) {
char* buf;//double<6C><65><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊ<EFBFBD>޷<EFBFBD><DEB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD> <20><>12.34 -> 1234
int dec, sign;//dec->С<><D0A1><EFBFBD><EFBFBD>ǰ<EFBFBD>м<EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD> sign->0Ϊ<30><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><31><CEAA><EFBFBD><EFBFBD>
buf = _fcvt(num, 2, &dec, &sign);
string str;
if (sign) str += "-";
int i;
for (i = 0; i < dec; i++) {
str += buf[i];
}
if (dec == 0)
str += "0";
if (strcmp(buf, "00") != 0) {
str += ".";
for (; i < strlen(buf); i++) {
str += buf[i];
}
}
return str;
}
std::string trim(const std::string& str) {
const std::string& whitespace = " \t";
const auto strBegin = str.find_first_not_of(whitespace);
if (strBegin == std::string::npos)
return ""; // no content
const auto strEnd = str.find_last_not_of(whitespace);
const auto strRange = strEnd - strBegin + 1;
return str.substr(strBegin, strRange);
}
std::string reduce(const std::string& str) {
const std::string& fill = " ";
const std::string& whitespace = " \t";
// trim first
auto result = trim(str);
// replace sub ranges
auto beginSpace = result.find_first_of(whitespace);
while (beginSpace != std::string::npos)
{
const auto endSpace = result.find_first_not_of(whitespace, beginSpace);
const auto range = endSpace - beginSpace;
result.replace(beginSpace, range, fill);
const auto newStart = beginSpace + fill.length();
beginSpace = result.find_first_of(whitespace, newStart);
}
return result;
}