📄 commontool.cpp
字号:
// CommonTool.cpp: implementation of the CCommonTool class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "CommonTool.h"
#include <afxtempl.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CCommonTool::CCommonTool()
{
}
CCommonTool::~CCommonTool()
{
}
// 检查输入的字符是否是数字字符
bool CCommonTool::CheckDigit(CString csTargetString,int nLength)
{
int nTmpLength = 0;
if ( nLength >= csTargetString.GetLength() )
{
nTmpLength = csTargetString.GetLength();
}
else
{
nTmpLength = nLength;
}
for ( int i = 0; i < nTmpLength; i++ )
{
TCHAR c = csTargetString.GetAt(i);
// 若输入的字符不是数字字符,则返回假
if ( !isdigit(c) )
{
return false;
}
}
return true;
}
// 分割字符串
int CCommonTool::SplitString(const CString &csInput,
const CString &csDelimiter,
CStringArray &aResults)
{
int nPos = -1;
int nNewPos = -1;
int nInputSize = csInput.GetLength();
int nDelimiterSize = csDelimiter.GetLength();
int nNumFound = 0;
CString csDstString = _T("");
CArray<int, int> aPositions; // int 型数组
nNewPos = csInput.Find(csDelimiter, 0); // 查找分界符的位置
if ( nNewPos < 0 )
{
aResults.Add( csInput );
return 0;
}
while ( nNewPos > nPos )
{
nNumFound++;
aPositions.Add( nNewPos ); // 将找到的分界符的位置存入数组
nPos = nNewPos;
nNewPos = csInput.Find( csDelimiter, nPos+nDelimiterSize );
}
for ( int i = 0; i <= aPositions.GetSize(); i++ )
{
if ( i == 0 )
{
csDstString = csInput.Mid( i, aPositions[i] ); // 获取目标字符串
}
else
{
int nOffset = aPositions[i-1] + nDelimiterSize;
if ( nOffset < nInputSize )
{
if ( i == aPositions.GetSize() )
{
csDstString = csInput.Mid(nOffset);
}
else if ( i > 0 )
{
csDstString = csInput.Mid( aPositions[i-1] + nDelimiterSize, aPositions[i] - aPositions[i-1] - nDelimiterSize );
}
else
{
// 这里,不需要做任何事情.
}
}
}
if ( csDstString.GetLength() >= 0 )
{
aResults.Add( csDstString ); // 将分割后的子字符串添加到结果数组
}
}
return nNumFound;
}
// 删除字符串左右的空格
void CCommonTool::DeleteLeftRightSpaceFromCString(CString csInput)
{
csInput.TrimLeft();
csInput.TrimRight();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -