判断字符是英文还是汉字
static bool isEnglishChar(char ch)
{
if (~(ch >> 8) == 0)
{
return false;
}
return true;
}
判断字符是英文还是汉字
static bool IsChinese(std::string strBuffer)
{
if (strBuffer == "")
{
return false;
}
for (int i = 0; i < strBuffer.length()-1; i++)
{
if (!isEnglishChar(strBuffer.at(i)))
{
return true;
}
}
return false;
}
获取剪切排除emoji表情的字符串
static std::string GetStringByCutEmoji(std::string strValue)
{
int size = strValue.size();
std::string result = "";
if (size > 0)
{
for (int i = 0; i < size; i++)
{
if (isEnglishChar(strValue[i]))
{
result.push_back(strValue[i]);
}
else
{
if ((unsigned)(strValue[i] & 0xf0) == 0xe0)
{
for (int j = 0; j < CHINESE_CHAR_LENGTH_UTF8; ++j)
{
result.push_back(strValue[i + j]);
}
i += CHINESE_CHAR_LENGTH_UTF8 - 1;
}
}
}
}
return result.c_str();
}
截取字符串
static std::string CutStringUtil(std::string util,int start, int end, bool isNeedPoint)
{
util = GetStringByCutEmoji(util);
if ( util.length() <= 0 || start >= end )
return util;
std::vector<std::string> _result;
int i = 0;
while (i < util.length())
{
if (!isEnglishChar(util.at(i)))
{
_result.push_back(util.substr(i, CHINESE_CHAR_LENGTH_UTF8));
i = i + CHINESE_CHAR_LENGTH_UTF8;
}
else
{
_result.push_back(util.substr(i, 1));
i = i + 1;
}
}
if (start >= 1 && _result.size() <= start)return util;
end = _result.size() >= end ? end : _result.size();
std::string temp = "";
int max = end * 2;
int curValue = 0;
int enIdx = 0;
for (int i = start; i < end; i++)
{
if (i > _result.size() - 1 || curValue >= max)
{
break;
}
temp += _result[i];
if (!IsChinese(_result[i].c_str()))
{
curValue++;
if (++enIdx % 2 == 0)
end++;
}
else
{
curValue += 2;
}
}
if (isNeedPoint)
{
if (_result.size() > end)
{
temp += "...";
}
}
return temp;
}
static bool IsINT(const char *str)
{
for (int i = 0; i < strlen(str); i++)
if (str[i] < '0' || str[i] > '9')
return false;
return true;
}