简体   繁体   English

使用C ++用字符串中的两个单引号替换单引号

[英]Replace single quote with two single quotes in a string using c++

The below function is working as expected. 下面的功能正在按预期方式工作。 But I think I can do it in an efficient way. 但是我认为我可以高效地做到这一点。

input = "Hello' Main's World";

Function Return Value "Hello'' Main''s World"; 函数返回值“ Hello” Main's World”;

string  ReplaceSingleQuote(string input)
{

int len = input.length();
int i = 0, j =0;
char str[255];
sprintf(str, input.c_str()); 
char strNew[255];

for (i = 0; i <= len; i++) 
{
    if (str[i] == '\'') 
    {
        strNew[j] = '\'';
        strNew[j+ 1] = '\'';
        j = j + 2;
    } else
    {
        strNew[j] = str[i];
        j = j + 1 ;
    }
}

return strNew;

}

Maybe use a std::stringstream : 也许使用std::stringstream

string  ReplaceSingleQuote(string input)
{
    stringstream s;
    for (i = 0; i <= input.length(); i++) 
    {
        s << input[i];
        if ( input[i] == '\'' )
           s << '\'';
    }
    return s.str();
}

A possibility (that will modify input ) is to use std::string::replace() and std::string::find() : 一种可能性(将修改input )是使用std::string::replace()std::string::find()

size_t pos = 0;
while (std::string::npos != (pos = input.find("'", pos)))
{
    input.replace(pos, 1, "\'\'", 2);
    pos += 2;
}

The obvious solution is: 显而易见的解决方案是:

std::string
replaceSingleQuote( std::string const& original )
{
    std::string results;
    for ( std::string::const_iterator current = original.begin();
            current != original.end();
            ++ current ) {
        if ( *current == '\'' ) {
            results.push_back( '\'');
        }
        results.push_back( *current );
    }
    return results;
}

Small variations might improve performance: 小变化可能会提高性能:

std::string
replaceSingleQuote( std::string const& original )
{
    std::string results(
        original.size() 
            + std::count( original.begin(), original.end(), '\''),
        '\'' );
    std::string::iterator dest = results.begin();
    for ( std::string::const_iterator current = original.begin();
            current != original.end();
            ++ current ) {
        if ( *current == '\'' ) {
            ++ dest;
        }
        *dest = *current;
        ++ dest;
    }
    return results;
}

might be worth trying, for example. 例如,可能值得尝试。 But only if you find the original version to be a bottleneck in your code; 但是,只有当您发现原始版本成为代码中的瓶颈时,才可以; there's no point in making something more complicated than necessary. 没有必要使事情变得不必要的复杂。

James Kanze's answer is a fine one. 詹姆斯·坎泽(James Kanze)的答案很好。 Just for the heck of it, though, I'll offer one that's a little more C++11ish. 不过,就此而言,我将提供一些C ++ 11ish版本。

string DoubleQuotes(string value)
{
    string retval;
    for (auto ch : value)
    {
        if (ch == '\'')
        {
            retval.push_back('\'');
        }
        retval.push_back(ch);
    }
    return retval;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM