在使用C++来处理字符串时,一般需要先将字符串两端无效的字符剔除。
本文整理了删除字符串两头指定的无效字符的代码实现,并且提供了删除中文空格和英文空格的具体代码。
/** 删除在source中两头的无效字符 */ // 无效的字符由target中的字符来指定 string Trim(const string& source, const string& target) { if (source.size() <= 0) return ""; size_t beginPos, endPos; size_t strLen = source.length(); for (beginPos = 0 ; beginPos < strLen; beginPos++) { if (std::string::npos == target.find(source.at(beginPos))) break; } for (endPos = strLen - 1 ; endPos > 0 ; endPos--) { if (std::string::npos == target.find(source.at(endPos))) break; } if (beginPos > endPos) return ""; return source.substr(beginPos, endPos - beginPos + 1); } // 删除字符两头的空格无效字符 std::string Trim(const std::string& source) { if (source.empty()) return source; size_t beginPos, endPos; size_t strLen = source.length(); for (beginPos = 0 ; beginPos < strLen; beginPos++) { // 如果该字符不在要删除的字符串中 if (!isspace((unsigned char)source[beginPos])) break; } for (endPos = strLen - 1 ; endPos > 0 ; endPos--) { // 如果该字符不在要删除的字符串中 if (!isspace((unsigned char)source[endPos])) break; } if (beginPos > endPos) return ""; return source.substr(beginPos, endPos - beginPos + 1); } /** 删除在中文字符串两头的中文空格 */ std::string TrimChineseSpace(const std::string& source) { if (source.size() < 2) return source; int head = 0; int tail = (int)source.size() - 2; // 确定字符串前面的中文空格的个数 while ((head <= (int)source.size() - 2) && 0xA1 == (unsigned char)source[head] && 0xA1 == (unsigned char)source[head+1]) head += 2; // 确定字符串前面的中文空格的个数 while (tail >= 0 && 0xA1 == (unsigned char)source[tail] && 0xA1 == (unsigned char)source[tail+1]) tail -= 2; if (tail < head) return ""; else return source.substr(head, tail - head + 2); } /** 删除在中文字符串两头的中文空格 */ std::string TrimChineseSpace(const std::string& source) { if (source.size() < 2) return source; int head = 0; int tail = (int)source.size() - 2; // 确定字符串前面的中文空格的个数 while ((head <= (int)source.size() - 2) && 0xA1 == (unsigned char)source[head] && 0xA1 == (unsigned char)source[head+1]) head += 2; // 确定字符串前面的中文空格的个数 while (tail >= 0 && 0xA1 == (unsigned char)source[tail] && 0xA1 == (unsigned char)source[tail+1]) tail -= 2; if (tail < head) return ""; else return source.substr(head, tail - head + 2); } /** 删除在中英文字符串两头的中英文空格 */ std::string TrimChEnSpace(const std::string& source) { if (source.size() < 2) return source; size_t head = 0; size_t tail = source.size()-1; // 确定字符串前面的中文空格的个数 while (head <= source.size()-2) { if (source[head] == ' ') { head ++; } else if (source[head] == (char)0xA1 && source[head+1] == (char)0xA1) { head += 2; } else { break; } } // 确定字符串后面的中英文空格的个数 while (tail > 0) { if (source[tail] == ' ') { tail --; } else if (source[tail-1] == (char)0xA1 && source[tail] == (char)0xA1) { tail -= 2; } else { break; } } if (tail < head) return ""; return source.substr(head, tail - head + 2); }
主要的实现思路是:分别对源字符串的头部和尾部进行逐个遍历,如果找到相应字符则指针偏移,最后就可利用字符指针来构成所生成的子字符串。