VC中使用StrTrim过滤指定字符

阅读 55

2022-07-27


编者:李国帅


背景原因:

VC中有没有删除掉字符串中特定字符的简单函数。

 

问题描述及期望效果:

使用简单函数,删除_!#等字符,如下

The string before calling StrTrim: _!ABCDEFG#

The string after calling StrTrim: BCDEFG。

所需资源:

VC,Shlwapi

Windows中有一个Shlwapi.dll文件,包含了大量的Windows字符串处理方法

 

解决方案:

用StrTrim(LPTSTR pszSource,LPCTSTR pszTrimChars)。

 

#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"

void main(void)
{
//String one
TCHAR buffer[] = TEXT("_!ABCDEFG#");
TCHAR trim[] = TEXT("#A!_\0");

cout << "The string before calling StrTrim: ";
cout << buffer;
cout << "\n";

StrTrim(buffer, trim);

cout << "The string after calling StrTrim: ";
cout << buffer;
cout << "\n";

}

 

输出:

OUTPUT:

- - - - - -

The string before calling StrTrim: _!ABCDEFG#

The string after calling StrTrim: BCDEFG

 

 

精彩评论(0)

0 0 举报