A project to emulate some, but not all, of the C#/Java StringBuilder classes using python's built-in list[str] class.
Tool | Version |
---|---|
Python | 3.13.2 |
VSCode | 1.97.1 |
PyCharm | 2024.3.3 |
Date | Description |
---|---|
2025-02-06 | Initial creation |
2025-02-07 | Simplify implementation using list[str] to hold contents |
2025-02-10 | Add more API documentation to README |
2025-02-11 | Add new methods to StringBuilder class |
2025-02-12 | Streamlined the append method |
Accepts an optional initial value
; otherwise, initializes to empty.
Example 1:
sbr = StringBuilder()
Example 2:
sbr = StringBuilder('ABCD')
Appends the specified string value
to the string buffer.
Example:
sbr = StringBuilder('ABCD')
sbr.append("EFGH")
sbr now contains "ABCDEFGH"
See size()
Deletes characters from the string buffer beginning at start_index
through and including end_index
Example:
sbr = StringBuilder('ABCDE' * 4)
start_index = 0
end_index = 3
sbr.delete(start_index, end_index)
After delete(0, 3), the buffer contains "EABCDEABCDEABCDE"
Returns either the index of the start of value
or -1 if value
does not exist in the buffer.
Example:
sbr = StringBuilder('ABCD')
idx = sbr.index_of("C") # returns 2
idx will contain a value of 2.
Inserts the specified string value
at the specified index
,
pushing the remainder of the entry to the right by
the length of the value
.
Example:
sbr = StringBuilder("A B C X Y Z ")
index = sbr.index_of('X ')
value_to_insert = 'J K L '
sbr.insert(index, value_to_insert)
after adding "J K L " at index = 6: sbr = "A B C J K L X Y Z "
Returns the index of the last occurrence of the specified string search value or -1 if not found.
sbr = StringBuilder('ABCDE' * 4)
search_for = 'CD'
idx = sbr.last_index_of(search_for)
The last occurrence of "CD" within "ABCDEABCDEABCDEABCDE" is at position 17
Removes length
characters from the buffer starting at index
.
Example:
sbr = StringBuilder("A B C J K L X Y Z ")
remove = 'X Y Z '
length = len(remove)
index = sbr.index_of(remove)
sbr.remove(index, length)
after removing "X Y Z " starting at 12 for a length of 6: sbr = "A B C J K L "
Replaces all occurrences of old_value
with new_value
.
Example:
sbr = StringBuilder('abcd' * 3)
find = 'b'
replace = 'B'
sbr.replace(find,replace)
After replacing "b" with "B", sbr = "aBcdaBcdaBcd"
Mimics the Java replace(int start, int end, String value)
API by replacing
the portion of the underlying value from start_index
(inclusive) to end_index
(exclusive)
with replacement_value
.
Example:
sbr = StringBuilder('ABCDEFG')
print(f'Original sbr = "{sbr}"')
replacement = "WXYZ"
start_index = sbr.index_of("C")
end_index = start_index + 1
sbr.replace_at(start_index, end_index, replacement)
Original sbr = "ABCDEFG"
After replace_at(2, 3, "WXYZ"), sbr = "ABWXYZDEFG"
Reverses the string inside the underlying buffer
Example:
sbr = StringBuilder('ABCDE')
print(f'Before revers(), sbr = "{sbr}"')
sbr.reverse()
print(f'After reverse(), sbr = "{sbr}"')
Before reverse(), sbr = "ABCDE"
After reverse(), sbr = "EDCBA"
Returns the current size of the buffer as an integer.
Example:
sbr = StringBuilder('abcd' * 3)
lth = sbr.size()
lth will contain the integer value 12.
Returns the StringBuffer contents as a python string (str).
Example:
sbr = StringBuilder('abcd' * 3)
x = sbr.to_string())
x will contain "abcdabcdabcd"