Last active
January 1, 2021 16:04
-
-
Save buildwithmh/d7cee6a9aeaf01e0d7915eb44f7d8e6d to your computer and use it in GitHub Desktop.
String Builder in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StringBuilder(object): | |
def __init__(self, val): | |
self.store = [val] | |
def __iadd__(self, value): | |
self.store.append(value) | |
return self | |
def __str__(self): | |
return "".join(self.store) | |
#Test | |
initial = 'value' | |
stringBuilder = StringBuilder(initial) | |
for _ in range(2000): | |
stringBuilder += 'value' | |
print(str(stringBuilder)) | |
#Thanks to | |
#https://grokbase.com/t/python/python-ideas/118sgkcgg0/create-a-stringbuilder-class-and-use-it-everywhere |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great, but is it possible to create some advanced features?
ex void Clear()
compared to, VS ... such as in
https://www.chilkatsoft.com/refdoc/pythonStringBuilderRef.html
id 140226925842928 - string testvaluevaluevaluevaluevaluevaluevaluevaluevaluevalue
VS
id 140227461358320 - string