string building

J

John Salerno

Out of curiosity, is there any kind of equivalent in Python to the
StringBuilder class in C#? Here's a quick description from the .NET
documentation:

"This class represents a string-like object whose value is a mutable
sequence of characters. The value is said to be mutable because it can
be modified once it has been created by appending, removing, replacing,
or inserting characters."

It's used for dealing with large strings that otherwise would cause
performance problems if used as regular string objects. I'm just
wondering if Python has something like this, or if it is even really
necessary. I know there is even some discussion on when exactly
StringBuilder becomes useful over regular strings, but I imagine at some
point it is good to use.
 
C

Chris Mellon

Out of curiosity, is there any kind of equivalent in Python to the
StringBuilder class in C#? Here's a quick description from the .NET
documentation:

"This class represents a string-like object whose value is a mutable
sequence of characters. The value is said to be mutable because it can
be modified once it has been created by appending, removing, replacing,
or inserting characters."

It's used for dealing with large strings that otherwise would cause
performance problems if used as regular string objects. I'm just
wondering if Python has something like this, or if it is even really
necessary. I know there is even some discussion on when exactly
StringBuilder becomes useful over regular strings, but I imagine at some
point it is good to use.


The StringIO module (and it's better performing counterpart,
cStringIO) present a string as a file-like object and are the
appropriate way to incrementally build large strings.
 
D

Duncan Booth

John said:
Out of curiosity, is there any kind of equivalent in Python to the
StringBuilder class in C#?

Yes, usually you use StringIO/cStringIO for this. It works for those
situations where you just want to append to a string as you build it.

The alternative is just to build up a list of strings and then concatenate
them all when you are done (using str.join).

If you really need mutable strings you can use the array module.
 
S

Sion Arrowsmith

John Salerno said:
Out of curiosity, is there any kind of equivalent in Python to the
StringBuilder class in C#? Here's a quick description from the .NET
documentation:

"This class represents a string-like object whose value is a mutable
sequence of characters. The value is said to be mutable because it can
be modified once it has been created by appending, removing, replacing,
or inserting characters."

You can get a long way with list of characters wrapped in list() and
"".join(). Seems to me like the main gotcha is to be careful with things
like:
mutable_string = list("initial\nvalue")
mutable_string[7] = "\r\n" and make sure you do:
mutable_string[7:8] = "\r\n"

(Although note that you have to spell find() and other string
methods "".join(mutable_string).find("value").)
 
S

sturlamolden

John said:
Out of curiosity, is there any kind of equivalent in Python to the
StringBuilder class in C#?

You can just append each string to a list and call "".join(list) on the
list. Here is a mock-up StringBuilder class:

class StringBuilder:
def __init__(self): self.strlist = list()
def Append(self,str): self.strlist.append(str)
def ToString(self): return "".join(self.strlist)

Implementing the rest of the .NET StringBuilder class is left as an
exercise:

http://msdn.microsoft.com/library/d...ml/frlrfsystemtextstringbuilderclasstopic.asp
http://msdn.microsoft.com/library/d.../frlrfsystemtextstringbuildermemberstopic.asp
 
J

John Salerno

John said:
Out of curiosity, is there any kind of equivalent in Python to the
StringBuilder class in C#? Here's a quick description from the .NET
documentation:

"This class represents a string-like object whose value is a mutable
sequence of characters. The value is said to be mutable because it can
be modified once it has been created by appending, removing, replacing,
or inserting characters."

It's used for dealing with large strings that otherwise would cause
performance problems if used as regular string objects. I'm just
wondering if Python has something like this, or if it is even really
necessary. I know there is even some discussion on when exactly
StringBuilder becomes useful over regular strings, but I imagine at some
point it is good to use.

Thanks guys, two interesting options to investigate! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top