• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 14412
  • Last Modified:

std::string replace function

I'm having issues find any example code on using std::replace.   I have
looked and what I'm finding doesn't make a lot of since.

std::string szMyString = "";
..
..
szMyString.append("EXEC sp_MyProc ");

Here I have a SQL statement that was passed in and I'm just wanting to save
the SQL statement off to an Audit table so we can't find when and what was
executed..

The SQL statement being passed to me is ExecMsg[255].   What I'm needing to
do is take this var, put it in the szMyString, then replace all the single
quotes with two single quotes, so SQL don't freak out on me when I go to
insert it.

szMyString.append(ExecMsg);

Now how do I replace all the single quotes with two single quotes?

These don't seem to work..   Looking around I don't understand exacly what
is needing passed in.

szMyString.replace(1, szMyString.length(), "'", "''");
szMyString.replace(1, "'", "''");

Thanx for your time..
0
Chizl
Asked:
Chizl
  • 7
  • 7
  • 2
  • +1
1 Solution
 
AxterCommented:
Here's an example code using the std::replace function:

#include <algorithm>

int main(int, char*)
{
     char data[] = "television+video+telephone+cassette recorder+";
     std::replace(data,data+strlen(data), '+','\n');
     printf("%s\n", data);

     system("pause");
     return 0;
}

0
 
ChizlAuthor Commented:
I'd rather use std::string's replace function, but I tried your way.

I added:
#include <algorithm>

I tried:
std::replace(ExecMsg,ExecMsg+strlen(ExecMsg), "'","''");

I'm getting:
:\code\C++\CDE v2\CDE\CDE.cpp(99) : error C2782: 'void __cdecl std::replace(_FI,_FI,const _Ty &,const _Ty &)' : template parameter '_Ty' is ambiguous
        could be 'char [3]'
        or       'char [2]'
0
 
jkrCommented:
'string::replace()' actually doesn't do what what you expect it to do - use

string single_quote = "\"";
string two_single_quotes = "\"\"";
int i = 0;
while ( string::npos != ( i = szMyString.find_first_of ( single_quote, i)) {

 szMyString.replace ( i, i + 1, two_single_quotes);

 i += 2;
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
proskigCommented:
Chizl,
note that although Axter's example is completely valid, it needs to be modified for your case. The thing is that Axter uses std::replace, which has signature similar to following:

void replace (ForwardIterator beg, ForwardIterator end, const T& oldValue, const T& newValue);

In case of a string it can replace single character for another single character. There is no way to replace one character for two!
What you can do is to use replace which is a member of basic_string. Simple example would be

szMyString.replace(szMyString.find("'"), 1, "''");

But it will replace only first occurance, what you need is to write a loop, which repeasts this operation.
0
 
proskigCommented:
sorry, I saw only first post, when started my own post
0
 
ChizlAuthor Commented:
jkr,
   That seems like a work around to what I'm wanting.

proskiq,
   That is what I'm looking for, but small problem.  hehe
   
   Lets say szMyString has "abcd'abcd'abcd'" in it.

   Run this:
   szMyString.replace(szMyString.find("'"), 1, "''");

   Return is:
   "abcd''abcd'abcd'"

Closer than I have been..  Need more than just the first one changed, I need all them changed.

I went through and tried changing things around, like changing the 1 to a 2 to see what happens.. Nothing..  I tried puting in the 4th parm szMyString.length(), but that ended up cutting my string in half..  
0
 
AxterCommented:
You really should be using the std::replace function for this.
You're only replacing a single charactor with another single charactor, so the std::replace function is optimize for this type of algorithm
0
 
AxterCommented:
Second example:

int main(int, char*)
{
     string szMyString = "television'video'telephone'cassette recorder'";
     std::replace(szMyString.begin(),szMyString.end(), '\'','\"');
     cout << szMyString << endl;

     system("pause");
     return 0;
}
0
 
ChizlAuthor Commented:
jkr,
  I see what your doing and if that is what replace needs to be used I understand now.   I tried that way, but it deletes most of the string, not just the single quote, and replace it with the double quote.

I'm doing different things with your code to see if I can fix the issue..
0
 
AxterCommented:
proskig,
>>note that although Axter's example is completely valid,
>>it needs to be modified for your case. The thing
>>is that Axter uses std::replace, which has signature
>>similar to following:

The questioner is only replacing a single charactor, so the method I posted is the correct one.


*****************************************************
He/She is talking about a single quote charactor (') VS a double quote charactor (")
*****************************************************

Chizl,
>>I'd rather use std::string's replace function, but I
>>tried your way.

If you use std::string's replace method, it will take more code, and it will be less optimize.
So it's a loose loose method.  You gain nothing, and loose optimisation and code simplicity.
0
 
ChizlAuthor Commented:
Axter,
   If you can tell me why I get compile errors, I would be happy to use..

C:\code\C++\CDE v2\CDE\CDE.cpp(100) : error C2782: 'void __cdecl std::replace(_FI,_FI,const _Ty &,const _Ty &)' : template parameter '_Ty' is ambiguous
        could be 'int'
        or       'char'
Error executing cl.exe.
0
 
AxterCommented:
Chizl,

Did you try it the way I posted it in the last example?

std::replace(szMyString.begin(),szMyString.end(), '\'','\"');

Use Single Quotes instead of double quotes.
0
 
ChizlAuthor Commented:
This is a basic non-mfc console display c++ application.
0
 
AxterCommented:
>>If you can tell me why I get compile errors, I would be
>>happy to use..

You're getting a compiler error because you used double quotes around the charactors.
Use single quotes as posted in my last example;
Copy and paste for best results.
0
 
ChizlAuthor Commented:
Yes, but I found the problem..

I need to replace 1 single quote with two single quotes.  Your example has 1 single quote and replaces it with 1 double quote.

When I changed your code to this is when I get the error..
std::replace(szExecMsg.begin(),szExecMsg.end(), '\'','\'\'');
0
 
AxterCommented:
Chizl,
>>I need to replace 1 single quote with two single quotes.
Sorry.  Disregard ALL my comments.  I misinterpret your requirements.

My method will not work.

I think jkr's method is more of what you need.
0
 
ChizlAuthor Commented:
I went ahead and went with the " instead of the '', so I can just past this..  

Thanx.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 7
  • 7
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now