Home » U++ Library support » U++ Core » no String::Replace() ?
no String::Replace() ? [message #11274] |
Wed, 29 August 2007 19:44 |
guido
Messages: 169 Registered: April 2006
|
Experienced Member |
|
|
Hi,
I'm adapting some code written to Apple CoreFoundation:
String es = s;
es.replace('\'', "''");
I came up with this:
String es = s;
int pos = 0;
while ( (pos = es.Find('\'', pos)) >= 0) {
es.Insert(pos, '\'');
pos += 2;
}
First, does this look correct?
Second, would you think Replace() method would be a usefull addition to String?
Guido
|
|
|
|
Re: no String::Replace() ? [message #20758 is a reply to message #11279] |
Sun, 05 April 2009 09:16 |
|
forlano
Messages: 1202 Registered: March 2006 Location: Italy
|
Senior Contributor |
|
|
luzr wrote on Thu, 30 August 2007 16:23 | Seem OK, but perhaps a little bit slow. I think the fast method would be to recreate String - insertion into String can be expensive.
I would do e.g.:
String es;
for(const char *q = s; *q; q++) {
es.Cat(*q);
if(*q == '\'')
es.Cat('\'');
}
As for inclusion of some Replace to String.h, well, why not, but perhaps not as member function. It is also the question how generic it should be (what you need is sort of special case that can be handled more effectively).
Mirek
|
Hello,
is there an U++ method/function for Replace? like
Replace(String old_s, String new_s, String source)
that substitute all the occurence of old_s in source with new_s?
I was not able to find it.
Thanks,
Luigi
[Updated on: Sun, 05 April 2009 09:26] Report message to a moderator
|
|
|
|
|
|
Re: no String::Replace() ? [message #21511 is a reply to message #20819] |
Sat, 23 May 2009 19:13 |
|
koldo wrote on Fri, 10 April 2009 18:27 | Hello all
If anybody needs them, in bazaar/sysinfo there are these functions:
...............
// Replace find with replace in str
String Replace(String str, String find, String replace)
Best regards
Koldo
|
OK... I created replace method too:
String replace_string(String& s1, String& find, String replace){
String string_result;
int start_pos = 0;
int found_pos = 0;
int find_len = find.GetCount();
int s1_count = s1.GetCount();
while(((found_pos=s1.Find(find, start_pos))!=-1)){
string_result.Cat(s1.Mid(start_pos, found_pos - start_pos));
string_result.Cat(replace);
start_pos = found_pos + find_len;
};
if(start_pos<s1.GetCount())
string_result.Cat(s1.Mid(start_pos));
return (string_result);
}
If is possible, may be add the most optimized replace function in String utilities!
It is very usefull method!
[Updated on: Sat, 23 May 2009 19:15] Report message to a moderator
|
|
|
|
|
Re: no String::Replace() ? [message #29210 is a reply to message #29147] |
Sun, 10 October 2010 20:01 |
|
mirek
Messages: 14039 Registered: November 2005
|
Ultimate Member |
|
|
OK, based on public request, I started inspired with Koldo's code and here is what I have added to String:
template <class B>
void AString<B>::Replace(const tchar *find, int findlen, const tchar *replace, int replacelen)
{
String r;
int i = 0;
const tchar *p = Begin();
for(;;) {
int j = Find(findlen, find, i);
if(j < 0)
break;
r.Cat(p + i, j - i);
r.Cat(replace, replacelen);
i = j + findlen;
}
r.Cat(p + i, GetCount() - i);
*this = r;
}
Please check the algo...
There are also
void Replace(const String& find, const String& replace) { Replace(~find, find.GetCount(), ~replace, replace.GetCount()); }
void Replace(const tchar *find, const tchar *replace) { Replace(find, strlen(find), replace, strlen(replace)); }
|
|
|
|
Re: no String::Replace() ? [message #29215 is a reply to message #29210] |
Mon, 11 October 2010 10:11 |
|
luzr wrote on Sun, 10 October 2010 20:01 | OK, based on public request, I started inspired with Koldo's code and here is what I have added to String:
template <class B>
void AString<B>::Replace(const tchar *find, int findlen, const tchar *replace, int replacelen)
{
String r;
int i = 0;
const tchar *p = Begin();
for(;;) {
int j = Find(findlen, find, i);
if(j < 0)
break;
r.Cat(p + i, j - i);
r.Cat(replace, replacelen);
i = j + findlen;
}
r.Cat(p + i, GetCount() - i);
*this = r;
}
Please check the algo...
There are also
void Replace(const String& find, const String& replace) { Replace(~find, find.GetCount(), ~replace, replace.GetCount()); }
void Replace(const tchar *find, const tchar *replace) { Replace(find, strlen(find), replace, strlen(replace)); }
|
Hi Mirek,
It fails to compile for me (Linux + GCC or Clang). It complains that Begin() and GetCount() are undefined names. It should be template <class B>
void AString<B>::Replace(const tchar *find, int findlen, const tchar *replace, int replacelen)
{
String r;
int i = 0;
const tchar *p = B::Begin(); // Added "B::"
for(;;) {
int j = Find(findlen, find, i);
if(j < 0)
break;
r.Cat(p + i, j - i);
r.Cat(replace, replacelen);
i = j + findlen;
}
r.Cat(p + i, B::GetCount() - i); // Added "B::"
*this = r;
}
Honza
|
|
|
|
|
Re: no String::Replace() ? [message #29361 is a reply to message #11274] |
Sat, 16 October 2010 02:14 |
zsolt
Messages: 698 Registered: December 2005 Location: Budapest, Hungary
|
Contributor |
|
|
BTW, some years ago I created a simple template based string replacer class. Replacing a lot of template variables in a long string many times is much faster that way.
The sample code:
#include <Core/Core.h>
#include <StringReplacer/StringReplacer.h>
CONSOLE_APP_MAIN
{
String template_str = "This is an example\n"
"!PARTNER! lives in !CITY!\n"
"Price of !PRODUCT.NAME! is !PRODUCT.PRICE!\n"
"This is some text at the end.";
VectorMap<String, Value> template_args;
template_args.Add("PARTNER", "John Doe");
template_args.Add("CITY", "Budapest");
template_args.Add("PRODUCT.NAME", "Disk");
template_args.Add("PRODUCT.PRICE", "12.34");
StringReplacer replacer;
replacer.SetTemplate(template_str);
String result;
replacer.Cat(template_args, result);
Cout() << result;
Cout() << "\n\nDone\n";
}
I can upload it here if somebody would use it.
|
|
|
Goto Forum:
Current Time: Sat Sep 21 06:56:48 CEST 2024
Total time taken to generate the page: 0.03323 seconds
|