[펌글] C++에서 자주 사용할 수 있는 string utilty 함수 모음
절름발이 프로그래머/C언어, C++ 과 Visual C++ / 2009. 3. 6. 15:14
http://hugman.tistory.com/entry/C-String-Utilts
-----------------------------------------------
namespace SU // SU represents 'String Utilities'
{
std::vector<std::string> split(std::string& text, std::string separators )
{
size_t n = text.length();
size_t start, stop;
std::vector<std::string> result;
start = text.find_first_not_of(separators);
while ((start >= 0) && (start < n)) {
stop = text.find_first_of(separators, start);
if ((stop < 0) || (stop > n)) stop = n;
result.push_back(text.substr(start, stop - start));
start = text.find_first_not_of(separators, stop+1);
}
return result;
}
std::string FindAndReplace (const std::string& source, const std::string target, const std::string replacement)
{
std::string str = source;
std::string::size_type pos = 0, // where we are now
found; // where the found data is
if (target.size () > 0) // searching for nothing will cause a loop
{
while ((found = str.find (target, pos)) != std::string::npos)
{
str.replace (found, target.size (), replacement);
pos = found + replacement.size ();
}
}
return str;
}; // end of FindAndReplace
bool IsTherePattern(const std::string source, const std::string pattern)
{
// Source string 에 pattern 이 존재하는지 안하는지 boolean value를 리턴한다.
if( source.find(pattern) != std::string::npos) { // exist?
return true;
}
else {
return false;
}
}
// Trim related methods
// Refer : http://www.codeproject.com/vcpp/stl/stdstringtrim.asp
// 약간 수정함
std::string TrimStart(std::string str, const char *delims) // trim delims in the start of string
{
str.erase(0, str.find_first_not_of(delims));
return str;
}
std::string TrimEnd(std::string str, const char *delims) // trim delims in the end of string
{
str.erase(str.find_last_not_of(delims) + 1);
return str;
}
std::string Trim(std::string str, const char *delims) // trim delims in the start and end of string
{
str = TrimStart(str, delims);
str = TrimEnd(str, delims);
return str;
}
// 소문자 대문자 변환
int ToLower(int c)
{
if(c>='A' && c<='Z') return c-'A'+'a';
else return c;
}
std::string ToLowerStrings(std::string str)
{
std::string result = "";
for(size_t i=0; i < str.length(); i++)
result += ToLower(str[i]);
return result;
}
int ToUpper(int c)
{
if(c>='a' && c<='z') return c-'a'+'A';
else return c;
}
std::string ToUpperStrings(std::string str)
{
std::string result = "";
for(size_t i=0; i <str.length(); i++)
result += ToUpper(str[i]);
return result;
}
#ifdef FOR_WINDOWS_ONLY
std::string CString2std_string(CString str)
{
return LPSTR(LPCTSTR(str));
}
char* CString2char_pointer(CString str)
{
return LPSTR(LPCTSTR(str));
}
CString GetCurrentPath()
{
HMODULE hModule;
hModule = ::GetModuleHandle(NULL); // handle of current module
ASSERT(hModule != 0);
CString strExeFileName;
VERIFY(::GetModuleFileName(hModule, strExeFileName.GetBuffer(_MAX_PATH),
_MAX_PATH));
strExeFileName.ReleaseBuffer();
char Drive[_MAX_DRIVE];
char Path[_MAX_PATH];
char Filename[_MAX_FNAME];
char Ext[_MAX_EXT];
// suha, for VC++ 2005, convert to char array from CString
char CharPath[_MAX_PATH];
for (int it = 0 ; it < _MAX_PATH ; it++)
{
CharPath[it] = strExeFileName.GetAt(it);
if (CharPath[it] == '\0') break;
}
_splitpath(CharPath, Drive, Path, Filename, Ext);
return CString(Drive)+CString(Path); // has trailing backslash
}
#endif
}