从字符串中删除某些字符 [英] remove certain characters from a string

查看:89
本文介绍了从字符串中删除某些字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个从字符串中删除某些字符的函数。对于

的例子,我经常用逗号来表示字符串...它们看起来像这样:


" 12,384"


我想取字符串,删除逗号并返回12384


最有效,最快捷的方式是什么?


谢谢,


布拉德

解决方案

Brad写道:


我正在写一个函数来从字符串中删除某些字符。对于

的例子,我经常用逗号来表示字符串...它们看起来像这样:


" 12,384"


我想取字符串,删除逗号并返回12384


最有效,最快捷的方式是什么?



可能是一个字符副本,跳过你想要的字符

删除。


-

Ian Collins。


Brad< br ** @ 16systems.comkirjutas:


我正在编写一个函数来删除字符串中的某些字符。对于

的例子,我经常用逗号来表示字符串...它们看起来像这样:


" 12,384"


我想取字符串,删除逗号并返回12384


最有效,最快捷的方式是什么?


谢谢,


Brad



std :: string s(" 12,384,586" ;);

std :: string :: size_type k = 0;

while((k = s.find('','',k))!= s.npos){

s.erase(k,1);

}


这个好吗?

Paavo


Paavo Helde写道:


>

std :: string s(" 12,384,586");

std :: string :: size_type k = 0;

while((k = s.find(' ','',k))!= s.npos){

s.erase(k,1);

}


这个好吗?

Paavo



谢谢,适合一个角色......何我会用

几个......它是这样的:


#include< iostream>


std :: string clean(std :: string the_string)

{

char bad [] = {''。'','','',' ''',''|'',''\ 0''};

std :: string :: size_type n = 0;

while((n = the_string.find(bad,n))!= the_string.npos)

{

the_string.erase(n,1);

}

std :: cout<< the_string<< std :: endl;

返回the_string;

}


int main()

{

clean(" 12,34.45 | 78 9");

返回0;

}


I''m writing a function to remove certain characters from strings. For
example, I often get strings with commas... they look like this:

"12,384"

I''d like to take that string, remove the comma and return "12384"

What is the most efficient, fastest way to approach this?

Thanks,

Brad

解决方案

Brad wrote:

I''m writing a function to remove certain characters from strings. For
example, I often get strings with commas... they look like this:

"12,384"

I''d like to take that string, remove the comma and return "12384"

What is the most efficient, fastest way to approach this?

Probably a character by character copy, skipping the characters you want
to remove.

--
Ian Collins.


Brad <br**@16systems.comkirjutas:

I''m writing a function to remove certain characters from strings. For
example, I often get strings with commas... they look like this:

"12,384"

I''d like to take that string, remove the comma and return "12384"

What is the most efficient, fastest way to approach this?

Thanks,

Brad

std::string s("12,384,586");
std::string::size_type k = 0;
while((k=s.find('','',k))!=s.npos) {
s.erase(k, 1);
}

this ok?
Paavo


Paavo Helde wrote:

>
std::string s("12,384,586");
std::string::size_type k = 0;
while((k=s.find('','',k))!=s.npos) {
s.erase(k, 1);
}

this ok?
Paavo

Thanks, works well with one character... How would I make it work with
several... like so:

#include <iostream>

std::string clean(std::string the_string)
{
char bad[] = {''.'', '','', '' '', ''|'', ''\0''};
std::string::size_type n = 0;
while ((n=the_string.find(bad, n))!=the_string.npos)
{
the_string.erase(n,1);
}
std::cout << the_string << std::endl;
return the_string;
}

int main()
{
clean("12,34.45|78 9");
return 0;
}


这篇关于从字符串中删除某些字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆