下面的代码是用来去除字符串两端空格或者字符串中所有的空格

bool EarseSpace(string &str)   //删除字符串两端的字符
{int first = 0;int end = 0;bool isFirst = false;bool isEnd = false;while (1){if (str.at(first) == ' ' && isFirst == false){str.erase(first, 1);first = 0;}else if (str.at(first) != ' '){isFirst = true;end = str.size() - 1;if (str.at(end) == ' ' && isEnd == false)str.erase(end, 1);else if (str.at(end) != ' ')isEnd = true;}if (isFirst && isEnd)return true;}return false;
}string EarseSpace(const char* szChar)
{string str(szChar);bool isTrue= EarseSpace(str);if (isTrue)return str;return szChar;
}bool EarseInSpace(string& str)//去除所有的空格,里面返回值有点问题
{int pos = 0;while (pos < str.size()){if (str.at(pos) == ' '){str.erase(pos, 1);pos = 0;}else{pos++;}}return true;
}string EarseInSpace(const char* szChar)
{string str(szChar);bool isTrue= EarseInSpace(str);if (isTrue)return str;return szChar;
}int main()
{string str2("  你好,    再见  ");string str3("  k   ");cout << str2.size() << endl;if (EarseInSpace(str2))cout << str2 <<" "<<str2.size()<< endl;cout << EarseSpace("    k 可以     ") << endl;cout << EarseInSpace(" nishi   你是  ") << endl;return 0;
}

为解析XML中的数据做准备