#StringBuilder comparison

13 messages · Page 1 of 1 (latest)

molten linden
#

Hey, I have this code that checks any String for a palindrome, wondering why The one where I use just StringBuilder doesn't work as intented? it always returns equal, is that because the method I use in the constructor affects the initial string as well?

potent oasisBOT
#

This post has been reserved for your question.

Hey @molten linden! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

crude mauve
#

you're using StringBuilder in both though? im confused

#

Strings are immutable, you can't change them

#

StringBuilder is not, it's mutable, and reverse mutates the content

molten linden
crude mauve
#

ah

#

i mean your 2 snippets just logically can't coexist

#

from your first snippet, sb.reverse must be modifying sb

#

but you seem to have just... forgotten that in the second snippet

molten linden
#

that's what I was asking... "is that because the method I use in the constructor affects the initial string as well?" so does it happen than sb 2 is given the value of the first sb, but at the same time sb stores its new value?

crude mauve
#

yes, otherwise your first snippet wouldn't work as sb.reverse() wouldn't affect sb in that scenario