📄 xstring.cpp
字号:
// XString.cpp Version 1.1 - article available at www.codeproject.com
//
// Author: Hans Dietrich
// hdietrich@gmail.com
//
// History
// Version 1.1 - 2007 August 7
// - Added _tcszdup()
// - Added _tcszndup()
// - Added _tcsccnt()
//
// Version 1.0 - 2007 June 25
// - Initial public release
//
// License:
// This software is released into the public domain. You are free to use
// it in any way you like, except that you may not sell this source code.
//
// This software is provided "as is" with no expressed or implied warranty.
// I accept no liability for any damage or loss of business that this
// software may cause.
//
// Public APIs:
// NAME DESCRIPTION
// ---------------- ------------------------------------------------------
// find
// _tcsistr() Find a substring in a string (case insensitive)
// _tcsccnt() Count number of occurrences of a character in a string
// removal
// _tcscrem() Remove character in a string (case sensitive)
// _tcsicrem() Remove character in a string (case insensitive)
// _tcsstrrem() Remove substring in a string (case sensitive)
// _tcsistrrem() Remove substring in a string (case insensitive)
// replace
// _tcscrep() Replace character in a string (case sensitive)
// _tcsicrep() Replace character in a string (case insensitive)
// _tcsistrrep() Replace one substring in a string with another
// substring (case insensitive)
// trim
// _tcsltrim() Removes (trims) leading whitespace characters from a string
// _tcsrtrim() Removes (trims) trailing whitespace characters from a string
// _tcstrim() Removes (trims) leading and trailing whitespace characters
// from a string
// copy
// _tcszdup() Allocates buffer with new, fills it with zeros, copies
// string
// _tcsnzdup() Allocates buffer with new, fills it with zeros, copies
// count characters from string to buffer
//
///////////////////////////////////////////////////////////////////////////////
// NOTE ABOUT PRECOMPILED HEADERS:
// This file does not need to be compiled with precompiled headers (.pch).
// To disable this, go to Project | Settings | C/C++ | Precompiled Headers
// and select "Not using precompiled headers". Be sure to do this for all
// build configurations.
//#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <crtdbg.h>
#include "XString.h"
//#include "XTrace.h"
#if 0 // -----------------------------------------------------------
#ifdef _DEBUG
void* operator new(size_t nSize, LPCSTR lpszFileName, int nLine);
#define DEBUG_NEW new(THIS_FILE, __LINE__)
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#endif // -----------------------------------------------------------
#pragma warning(disable : 4127) // conditional expression is constant (_ASSERTE)
#pragma warning(disable : 4996) // disable bogus deprecation warning
///////////////////////////////////////////////////////////////////////////////
//
// _tcscrep()
//
// Purpose: Replace character in a string (case sensitive)
//
// Parameters: str - pointer to string; upon return, str will be updated
// with the character replacements
// chOld - character to look for
// chNew - character to replace it with
//
// Returns: TCHAR * - Pointer to the updated string. Because the
// modification is done in place, the pointer
// returned is the same as the pointer passed
// as the input argument.
//
TCHAR * _tcscrep(TCHAR *str, TCHAR chOld, TCHAR chNew)
{
if (!str)
return str;
TCHAR *str1 = str;
while (*str1)
{
if (*str1 == chOld)
*str1 = chNew;
str1++;
}
return str;
}
///////////////////////////////////////////////////////////////////////////////
//
// _tcsicrep()
//
// Purpose: Replace character in a string (case insensitive)
//
// Parameters: str - pointer to string; upon return, str will be updated
// with the character replacements
// chOld - character to look for
// chNew - character to replace it with
//
// Returns: TCHAR * - Pointer to the updated string. Because the
// modification is done in place, the pointer
// returned is the same as the pointer passed
// as the input argument.
//
TCHAR * _tcsicrep(TCHAR *str, TCHAR chOld, TCHAR chNew)
{
if (!str)
return str;
size_t nLen = _tcslen(str);
TCHAR *newstr = new TCHAR [nLen+1];
ZeroMemory(newstr, nLen+1);
_tcscpy(newstr, str);
_tcslwr(newstr);
TCHAR oldstr[2] = { chOld, _T('\0') };
_tcslwr(oldstr);
TCHAR *cp = newstr;
while (*cp)
{
if (*cp == oldstr[0])
str[cp-newstr] = chNew;
cp++;
}
delete [] newstr;
return str;
}
///////////////////////////////////////////////////////////////////////////////
//
// _tcsicrem()
//
// Purpose: Remove character in a string (case insensitive)
//
// Parameters: str - pointer to string; upon return, str will be updated
// with the character removals
// ch - character to remove
//
// Returns: TCHAR * - Pointer to the updated string. Because the
// modification is done in place, the pointer
// returned is the same as the pointer passed
// as the input argument.
//
TCHAR * _tcsicrem(TCHAR * str, TCHAR ch)
{
if (!str)
return str;
size_t nLen = _tcslen(str);
TCHAR *newstr = new TCHAR [nLen+1];
ZeroMemory(newstr, nLen+1);
_tcscpy(newstr, str);
_tcslwr(newstr);
TCHAR chstr[2] = { ch, _T('\0') };
_tcslwr(chstr);
TCHAR *cp1 = str;
TCHAR *cp2 = newstr;
while (*cp2)
{
if (*cp2 != chstr[0])
*cp1++ = str[cp2-newstr];
cp2++;
}
*cp1 = _T('\0');
delete [] newstr;
return str;
}
///////////////////////////////////////////////////////////////////////////////
//
// _tcscrem()
//
// Purpose: Remove character in a string (case sensitive)
//
// Parameters: str - pointer to string; upon return, str will be updated
// with the character removals
// ch - character to remove
//
// Returns: TCHAR * - Pointer to the updated string. Because the
// modification is done in place, the pointer
// returned is the same as the pointer passed
// as the input argument.
//
TCHAR * _tcscrem(TCHAR * str, TCHAR ch)
{
if (!str)
return str;
TCHAR *cp1 = str;
TCHAR *cp2 = str;
while (*cp2)
{
if (*cp2 != ch)
*cp1++ = *cp2;
cp2++;
}
*cp1 = _T('\0');
return str;
}
///////////////////////////////////////////////////////////////////////////////
//
// _tcsistrrem()
//
// Purpose: Remove substring in a string (case insensitive)
//
// Parameters: str - pointer to string; upon return, str will be updated
// with the substring removals
// substr - substring to remove
//
// Returns: TCHAR * - Pointer to the updated string. Because the
// modification is done in place, the pointer
// returned is the same as the pointer passed
// as the input argument.
//
TCHAR * _tcsistrrem(TCHAR * str, const TCHAR *substr)
{
if (!str)
return str;
if (!substr)
return str;
TCHAR *target = NULL;
size_t nSubstrLen = _tcslen(substr);
TCHAR *cp = str;
while ((target = _tcsistr(cp, substr)) != NULL)
{
_tcscpy(target, target + nSubstrLen);
cp = target;
}
return str;
}
///////////////////////////////////////////////////////////////////////////////
//
// _tcsstrrem()
//
// Purpose: Remove substring in a string (case sensitive)
//
// Parameters: str - pointer to string; upon return, str will be updated
// with the substring removals
// substr - substring to remove
//
// Returns: TCHAR * - Pointer to the updated string. Because the
// modification is done in place, the pointer
// returned is the same as the pointer passed
// as the input argument.
//
TCHAR * _tcsstrrem(TCHAR * str, const TCHAR *substr)
{
if (!str)
return str;
if (!substr)
return str;
TCHAR *target = NULL;
size_t nSubstrLen = _tcslen(substr);
TCHAR *cp = str;
while ((target = _tcsstr(cp, substr)) != NULL)
{
_tcscpy(target, target + nSubstrLen);
cp = target;
}
return str;
}
///////////////////////////////////////////////////////////////////////////////
//
// _tcsistr()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -