Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » no String::Replace() ?
no String::Replace() ? [message #11274] Wed, 29 August 2007 19:44 Go to next message
guido is currently offline  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 #11279 is a reply to message #11274] Thu, 30 August 2007 16:23 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
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
Re: no String::Replace() ? [message #20758 is a reply to message #11279] Sun, 05 April 2009 09:16 Go to previous messageGo to next message
forlano is currently offline  forlano
Messages: 1182
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 #20761 is a reply to message #20758] Sun, 05 April 2009 09:45 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
No, there is none.

Mirek
Re: no String::Replace() ? [message #20819 is a reply to message #11274] Fri, 10 April 2009 17:27 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
Hello all

If anybody needs them, in bazaar/sysinfo there are these functions:

// Appends a file after other
// It could be beside FileCopy, FileMove and FileDelete
bool FileCat(const char *file, const char *appendFile)

// Convert a long into a String
// It could be beside FormatInteger, FormatUnsigned, FormatDouble, FormatBool and FormatInt64
String FormatLong(long a)

// Replace find with replace in str
String Replace(String str, String find, String replace)

Best regards
Koldo


Best regards
IƱaki
Re: no String::Replace() ? [message #20824 is a reply to message #20819] Fri, 10 April 2009 20:12 Go to previous messageGo to next message
forlano is currently offline  forlano
Messages: 1182
Registered: March 2006
Location: Italy
Senior Contributor
koldo wrote on Fri, 10 April 2009 17:27

Hello all
// Replace find with replace in str
String Replace(String str, String find, String replace)

Best regards
Koldo


Very good! Thanks,
Luigi
Re: no String::Replace() ? [message #21511 is a reply to message #20819] Sat, 23 May 2009 19:13 Go to previous messageGo to next message
tojocky is currently offline  tojocky
Messages: 607
Registered: April 2008
Location: UK
Contributor

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 #25691 is a reply to message #21511] Mon, 08 March 2010 02:18 Go to previous messageGo to next message
alendar is currently offline  alendar
Messages: 47
Registered: January 2010
Location: Idaho, USA
Member
Used it. Thanks, koldo.

cd7651feeb698f6ac6cec1f6deda5e5b
Re: no String::Replace() ? [message #29147 is a reply to message #21511] Thu, 07 October 2010 15:13 Go to previous messageGo to next message
sevenjay is currently offline  sevenjay
Messages: 30
Registered: October 2008
Location: Taiwan
Member
I agree with Tojocky to add some useful functions in String utilities.
I use Koldo's code now.
Thx.
Re: no String::Replace() ? [message #29210 is a reply to message #29147] Sun, 10 October 2010 20:01 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
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 #29212 is a reply to message #29210] Mon, 11 October 2010 09:39 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1307
Registered: September 2007
Ultimate Contributor
Nice addition, I was missing it many times and coded about 20 variants of it Smile

Another stuff I didn't find.... some ToInteger(), ToDouble()... and so on, or maybe as external functions like ScanHexString().
Or they're maybe there hidden somewhere ? Smile

Max
Re: no String::Replace() ? [message #29215 is a reply to message #29210] Mon, 11 October 2010 10:11 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

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 #29216 is a reply to message #29215] Mon, 11 October 2010 12:19 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Thank you, fixed.

Mirek
Re: no String::Replace() ? [message #29360 is a reply to message #29212] Sat, 16 October 2010 02:02 Go to previous messageGo to next message
zsolt is currently offline  zsolt
Messages: 693
Registered: December 2005
Location: Budapest, Hungary
Contributor
mdelfede wrote on Mon, 11 October 2010 09:39


Another stuff I didn't find.... some ToInteger(), ToDouble()... and so on, or maybe as external functions like ScanHexString().
Or they're maybe there hidden somewhere ? Smile



They are hidden in Core/Convert.h Smile
StrInt(), StrDbl()

Parsing a hex string should be very simple using a for loop.

[Updated on: Sat, 16 October 2010 02:03]

Report message to a moderator

Re: no String::Replace() ? [message #29361 is a reply to message #11274] Sat, 16 October 2010 02:14 Go to previous message
zsolt is currently offline  zsolt
Messages: 693
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.
Previous Topic: Compile errror in ctrllib/update.cpp
Next Topic: PROPOSAL: SerializeStore helper
Goto Forum:
  


Current Time: Thu Mar 28 14:05:01 CET 2024

Total time taken to generate the page: 0.01587 seconds