將字串補足字元到一定長度
如果原本字串長度不足要求長度
則補上設定的字元
如果原本字串長度大於要求長度
則切斷他
1: namespace StringPlus
2: {
3: /// Right-aligns characters in this string, padding on the left with the symbol character.
4: std::string PadLeft (const std::string& szSrc, int length , char symbol)
5: {
6: if(szSrc.size()>length)
7: return szSrc.substr(szSrc.size()-length , length);
8:
9: if(szSrc.size()==length)
10: return szSrc;
11:
12: std::stringstream _ss;
13: _ss << std::setfill(symbol) << std::setw(length) << std::setiosflags(std::ios::right) << szSrc;
14: return _ss.str();
15: }
16:
17: /// Left-aligns characters in this string, padding on the right with the symbol character.
18: std::string PadRight(const std::string& szSrc, int length , char symbol)
19: {
20: if(szSrc.size()>length)
21: return szSrc.substr(0, length);
22:
23: if(szSrc.size()==length)
24: return szSrc;
25:
26: std::stringstream _ss;
27: _ss << std::setfill(symbol) << std::setw(length) << std::setiosflags(std::ios::left) << szSrc;
28: return _ss.str();
29: }
30: }