473,379 Members | 1,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

string building

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.
Apr 3 '06 #1
5 3847
On 4/3/06, John Salerno <jo******@nospamgmail.com> wrote:
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.
Apr 3 '06 #2
John Salerno wrote:
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.
Apr 3 '06 #3
John Salerno <jo******@NOSPAMgmail.com> wrote:
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 -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
Apr 3 '06 #4

John Salerno wrote:
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/de...classtopic.asp
http://msdn.microsoft.com/library/de...mberstopic.asp

Apr 3 '06 #5
John Salerno wrote:
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! :)
Apr 3 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
3
by: Dave Byron | last post by:
I am having trouble with DBNull's from my SQL server. I am building/converting a asset web app from Access and my db has nulls on various fields . I have tried searching newsgroups and tried to get...
4
by: John | last post by:
Hi What are the advantages of a stringbuilder compared to a string? Thanks Regards
5
by: feng | last post by:
OK, all I want is writting a string that represents an XML document into a file, say C:\test.xml. This kind of task used to be so easy with vb6. But now, with VB.Net, it seems turned into a big...
12
by: DumberThanSnot | last post by:
is there a faster way to copy an ArrayList of strings to a string other than a tight loop. In it's most simple terms, I'm currently using something like this... ---------------------------- ...
35
by: jacob navia | last post by:
Hi guys! I like C because is fun. So, I wrote this function for the lcc-win32 standard library: strrepl. I thought that with so many "C heads" around, maybe we could improve it in a...
8
by: Jacob Arthur | last post by:
How would I go about using a custom select string that is passed from a form to the SelectCommand parameter of SqlDataSource? I tried: SelectCommand = "<% Request.Form("hdnSelect") %>" but I...
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
9
by: Neal Barney | last post by:
I have a C program which runs on a device using a Zilog Z180 microprocessor. While it can address 1MB of RAM, it can only address 64KB at any given time. And of that only 16KB can be used for...
13
by: Tony Johansson | last post by:
Hello! I read in a book and here is a question and the answer that I'm not satisfied with. When should you use the StringBuilder class instead of the String class. 1.When building a string from...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.