I know .NET (and therefore VB.NET) strings are immutable. However, I'm using VBA 7.0 in Excel 2010. Are strings there immutable? I'm doing a lot of string processing and for small quantities, some (direct) string manipulation is fine, but I'm worried it won't scale - since every additional character moved from one string to another might create yet another instance of the string.
Strings are not really immutable. They are just publicly immutable. It means you cannot modify them from their public interface.
Variable length Thus, the maximum string length that can be handled by VBA is 2,147,483,647 characters.
Advertisements. Strings are a sequence of characters, which can consist of either alphabets, numbers, special characters, or all of them. A variable is said to be a string if it is enclosed within double quotes " ".
In Java, C#, JavaScript, Python and Go, strings are immutable.
While VB.NET strings are immutable, as mandated by System.String, VBA (including VB6?) strings can be mutated (such as with Mid$
). See Alex K's answer and note the StrPtr
result after the operations.
Original answer; supported by documentation, in opposition to a counter-example.
VBA strings are immutable.
Just as with VB.NET, there is no way to "replace part of" or "append to" a string without creating a new string. Whether or not this matters - as modern computers are pretty darn fast - depends on the actual algorithm, data, and environment.
Unlike .NET documentation, such a behavior reference for VBA is [becoming] difficult to track down. From MS-VBAL: 2.1 Data Values and Value Types, we find this rare little gem
Individual data values are immutable. This means that there are no defined mechanisms available within a VBA Environment that can cause a data value to change into another data value.
where Strings represent "individual data values".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With