如何去掉一个字符串中的所有不可见字符?

dfpgb 2007-12-20 01:11:18
如何去掉一个字符串中的所有不可见字符?
...全文
1365 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
shinaterry 2008-03-12
  • 打赏
  • 举报
回复
-_-!!!
dfpgb 2007-12-21
  • 打赏
  • 举报
回复
全是高手~呵呵~谢谢大家~
月亮上的猪~你再加把劲就全是星星了~哈~
Waiting4you 2007-12-21
  • 打赏
  • 举报
回复
当然,WC的时候功效更好
i_love_pc 2007-12-21
  • 打赏
  • 举报
回复
洗脚还有这个功效??!!
Waiting4you 2007-12-20
  • 打赏
  • 举报
回复
洗完脚突然兴致大发,写个STL味道浓一点的,呵呵:)

#include <algorithm> 
#include <functional>
#include <ctype.h>

void __fastcall TForm1::Button1Click(TObject *Sender)
{
using namespace std;
char *str="abc\t\r\nefg\bh\rig";
char *pLast = remove_if(str,str+strlen(str),not1(ptr_fun(isprint)));
*pLast = 0;
ShowMessage(str);
}
Waiting4you 2007-12-20
  • 打赏
  • 举报
回复
不惑的提示 + 杰子的代码
然后我来补全:

#include <algorithm>
#include <ctype.h>
bool IsNotPrint(char c)
{
return !isprint(c) && c!=0;
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
char *str="abc\t\r\nefg\bh\r";
std::remove_if(str,str+strlen(str)+1,IsNotPrint);
ShowMessage(str);
}
i_love_pc 2007-12-20
  • 打赏
  • 举报
回复
嗯,还是不惑老大见多识广!

isprint, iswprint, _ismbcprint

Header File

ctype.h, wctype.h, mbstring.h

Category

Classification Routines

Prototype

int isprint(int c);

int iswprint(wint_t c);

int _ismbcprint(unsigned int c);

Description

Tests for printing character.

isprint is a macro that classifies ASCII-coded integer values by table lookup. The macro is affected by the current locale’s LC_CTYPE category. For the default C locale, c is a printing character including the blank space (‘ ‘).

You can make this macro available as a function by undefining (#undef) it.

Return Value

isprint returns nonzero if c is a printing character.

#include <stdio.h>
#include <ctype.h>

int main(void)
{
char c = 'C';

if (isprint(c))
printf("%c is a printable character\n",c);
else
printf("%c is not a printable character\n",c);

return 0;
}
cczlp 2007-12-20
  • 打赏
  • 举报
回复
用函数判断
int isprint( int c );

int iswprint( wint_t c );

我不懂电脑 2007-12-20
  • 打赏
  • 举报
回复
做一个不可见字符表。
把str和不可见字符表比较,去掉相同的字符。
dfpgb 2007-12-20
  • 打赏
  • 举报
回复
月亮上的猪~:
非常感谢你哦~你很热心呢~
你上面的例子是要帮我找出我想替换的字符的位置对吧?可是这样只是第一步吧?那后面呢?

嗯,我现在是这么做的,希望大家看看有问题吗,或者有更好的方法吗?谢谢杰子的启发~


//成员变量
set<char> m_setChar;
//构造函数
__fastcall TFormMain::TFormMain(TComponent* Owner)
: TForm(Owner)
{
for(int i = 32;i<127;i++)
{
m_setChar.insert(i);
}
}

//我的函数体对我接受到的字符串开始处理
for(int m = 1;m<=strReceiveText.Length();m++)
{
if(m_setChar.find(strReceiveText[m])== m_setChar.end())
{
strReceiveText.Delete(m,1);
m--;
}
}



ydlchina 2007-12-20
  • 打赏
  • 举报
回复
俺写了个查找换行符的例子,看看是否符合你的需要
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString str = "asbc\refg";

int i = str.Pos("\r");//换行符的位置
ShowMessage(str+ " " + IntToStr(i));
}
dfpgb 2007-12-20
  • 打赏
  • 举报
回复
风归叶:
不能肯定的,像'\xFE','\xC0'等等甚至'\0'什么乱七八糟的都有..个数也不一定长度也不一定..
月亮上的猪:
谢谢你哦~你又来了~呵呵~你是让我举不可见字符的例子吗?那就像上面了~
杰子:
空格不算啊,我指的不可见就算是不可打印的吧应该

嗯,大家有什么好的办法吗,越简单越好...呵呵.



PS:网速突然N慢.....回复框现在才刷新出来...郁闷....

i_love_pc 2007-12-20
  • 打赏
  • 举报
回复
如果仅是ASCII(0~127)范围的话,0~31和127算是不可见字符(32空格算不算?),比较ASCII码值可以吗?
ydlchina 2007-12-20
  • 打赏
  • 举报
回复
举个例子看啊。
CACACACACA 2007-12-20
  • 打赏
  • 举报
回复
晕,哪些为不可见的能肯定吗?

13,826

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧