问题描述
让我们说我有一个std :: string,我想用>替换所有'','
字符或,,即A,B,C。 - > A或B或C。
遵循最佳方式吗?
int idx;
while((idx = str.find_first_of( '',''))> = 0){
str.replace(idx,1,"");
str.insert(idx," ;或者是;);
}
(不幸的是,Stroustrup在这里并没有太多启发。)
-
Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我
ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。
Let''s say I have a std::string, and I want to replace all the '',''
characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the
following the best way to do it?
int idx;
while( (idx=str.find_first_of('','')) >= 0 ) {
str.replace( idx, 1, "" );
str.insert( idx, " or " );
}
(Stroustrup wasn''t too illuminating here, unfortunately.)
--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.
推荐答案
看看这是否适合你:
// /
///将字符串中子字符串的所有出现替换为另一个
///字符串,就地。
// /
/// @param s要替换的字符串。将被修改。
/// @param sub要替换的子字符串。
/// @param用其他字符串替换子字符串。
///
/// @return替换后的字符串。
inline std :: string&
replacein(std :: string& s,const std :: string& sub,
const std :: string& other)
{
断言(!sub.empty());
size_t b = 0;
for( ;;)
{
b = s.find(sub,b);
if(b == s.npos)break;
s.replace(b, sub.size(),other);
b + = other.size();
}
返回s;
}
-
见到你。
See if this will work for you:
///
/// Replace all occurences of a substring in a string with another
/// string, in-place.
///
/// @param s String to replace in. Will be modified.
/// @param sub Substring to replace.
/// @param other String to replace substring with.
///
/// @return The string after replacements.
inline std::string &
replacein(std::string &s, const std::string &sub,
const std::string &other)
{
assert(!sub.empty());
size_t b = 0;
for (;;)
{
b = s.find(sub, b);
if (b == s.npos) break;
s.replace(b, sub.size(), other);
b += other.size();
}
return s;
}
--
Be seeing you.
-
从来没有想过为什么胡萝卜比橙子更橙?? br />
--
Never wondered why a carrot is more orange than an orange?
为了更多功能,我已经改变了你的''角色以找到''
to''string to find'':
#include< iostream>
#include< string>
/ *如果''来自''匹配''''或''来自''为空,* /
/ *不解析's'',则返回std :: string :: npos * /
/ * * /
/ *否则返回已完成的替换次数* /
/ * * /
std :: string :: size_type repl(std :: string& s,
co nst std :: string& from,
const std :: string& to)
{
std :: string :: size_type cnt( std :: string :: npos);
if(from!= to&& !from.empty())
{
std :: string :: size_type pos1(0);
std :: string :: size_type pos2(0);
const std :: string :: size_type from_len(from.size());
const std :: string :: size_type to_len(to .size());
cnt = 0;
while((pos1 = s.find(from,pos2))!= std :: string :: npos)
{
s.replace(pos1,from_len,to);
pos2 = pos1 + to_len;
++ cnt;
}
}
返回cnt;
}
int main()
{
std :: string s(" A,B,C");
const std :: string old_seq(",");
const std :: string new_seq(" or);
std :: cout<< 原始字符串:\ n"
<< \ n \ nn;
const std :: string :: size_type count(repl(s,old_seq,new_seq));
const std :: string dq(count> 0,''"'');
const bool解析(count!= std :: string :: npos);
const bool found(解析&&> 0);
if(parsed)
{
std :: cout<< count
<< "序列的出现\"" << old_seq<< " \""
<< (count?" replace with sequence":find)
<< dq<< (count?new_seq:"")<< dq
<< \ n \ nn;
}
其他
std :: cout<< 没什么可改变的,但没有什么可改变的;
std :: cout<< (解析&&发现
?std :: string(" New string:\ n" + dq + s + dq)
:没有变化制作了)
<< ''\ n'';
返回0;
}
原始字符串:
A,B,C
2出现序列,用序列替换或
新字符串:
A或B或C
HTH,
-Mike
For more versatility, I''ve changed your ''character to find''
to ''string to find'':
#include <iostream>
#include <string>
/* If ''from'' matches ''to'' or ''from'' is empty, */
/* does not parse ''s'', returns std::string::npos */
/* */
/* Otherwise returns number of replacements done */
/* */
std::string::size_type repl(std::string& s,
const std::string& from,
const std::string& to)
{
std::string::size_type cnt(std::string::npos);
if(from != to && !from.empty())
{
std::string::size_type pos1(0);
std::string::size_type pos2(0);
const std::string::size_type from_len(from.size());
const std::string::size_type to_len(to.size());
cnt = 0;
while((pos1 = s.find(from, pos2)) != std::string::npos)
{
s.replace(pos1, from_len, to);
pos2 = pos1 + to_len;
++cnt;
}
}
return cnt;
}
int main()
{
std::string s("A,B,C");
const std::string old_seq(",");
const std::string new_seq(" or ");
std::cout << "Original string:\n"
<< ''"'' << s << ''"''
<< "\n\n";
const std::string::size_type count(repl(s, old_seq, new_seq));
const std::string dq(count > 0, ''"'');
const bool parsed(count != std::string::npos);
const bool found(parsed && count > 0);
if(parsed)
{
std::cout << count
<< " occurences of sequence \"" << old_seq << "\""
<< (count ? " replaced with sequence " : " found")
<< dq << (count ? new_seq : "") << dq
<< "\n\n";
}
else
std::cout << "Nothing to change\n\n";
std::cout << (parsed && found
? std::string("New string:\n" + dq + s + dq)
: "No changes made")
<< ''\n'';
return 0;
}
Original string:
"A,B,C"
2 occurences of sequence "," replaced with sequence " or "
New string:
"A or B or C"
HTH,
-Mike
这篇关于快速std :: string问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!