Solved

std::string replace function

Posted on 2002-04-18
17
14,395 Views
Last Modified: 2011-08-18
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
Comment
Question by:Chizl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
  • 2
  • +1
17 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 6950936
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
 
LVL 4

Author Comment

by:Chizl
ID: 6950960
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
 
LVL 86

Expert Comment

by:jkr
ID: 6950969
'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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:proskig
ID: 6950970
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
 
LVL 5

Expert Comment

by:proskig
ID: 6950974
sorry, I saw only first post, when started my own post
0
 
LVL 4

Author Comment

by:Chizl
ID: 6951102
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
 
LVL 30

Expert Comment

by:Axter
ID: 6951124
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
 
LVL 30

Expert Comment

by:Axter
ID: 6951141
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
 
LVL 4

Author Comment

by:Chizl
ID: 6951217
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
 
LVL 30

Expert Comment

by:Axter
ID: 6951220
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
 
LVL 4

Author Comment

by:Chizl
ID: 6951229
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
 
LVL 30

Accepted Solution

by:
Axter earned 200 total points
ID: 6951232
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
 
LVL 4

Author Comment

by:Chizl
ID: 6951238
This is a basic non-mfc console display c++ application.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6951239
>>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
 
LVL 4

Author Comment

by:Chizl
ID: 6951247
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
 
LVL 30

Expert Comment

by:Axter
ID: 6951288
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
 
LVL 4

Author Comment

by:Chizl
ID: 6951312
I went ahead and went with the " instead of the '', so I can just past this..  

Thanx.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question