Java – questions about reversing strings

I'm trying to do a simple string operation The input is "murder". I want "murder"

I tried this

String str = "murder";
StringBuffer buf = new StringBuffer(str);
// buf is Now "murder",so i append the reverse which is "redrum"
buf.append(buf.reverse());
System.out.println(buf);

But now I get "red rumredrum" instead of "murder Redrum"

Who can explain what's wrong with my program? thank you.

Solution

Short answer

This line:

buf.append(buf.reverse());

Basically do the following:

buf.reverse();    // buf is Now "redrum"
buf.append(buf);

That's why you get "red rumred rum"

That is, buf Reverse () does not return a new StringBuffer that is the opposite of buff After it reverses, it returns buf!

There are many ways to "fix" this, but the simplest way is to explicitly create a new StringBuffer for inversion, as shown below:

buf.append(new StringBuffer(str).reverse());

Insight: compare string and StringBuffer

Strings in Java are immutable On the other hand, StringBuffer is variable (which is why you can, among other things, attach things)

This is why using string, the conversion method does return a new string That's why such things are "wrong"

String str = "murder";
str.toUpperCase(); // this is "wrong"!!!
System.out.println(str); // still "murder"

Instead, you want to:

String str = "murder";
str = str.toUpperCase(); // YES!!!
System.out.println(str); // Now "MURDER"!!!

However, the situation is similar to StringBuffer Most StringBuffer methods return StringBuffer, but they return the same instance! They do not return a new StringBuffer instance In fact, you are free to discard "results" because these methods have done what they do to the instance they call through various mutations (i.e. side effects)

These methods may be declared invalid, but they basically return this reason; On the contrary, it promotes method chaining and allows you to write as follows:

sb.append(thisThing).append(thatThing).append(oneMoreForGoodMeasure);

Related issues

> Method chaining – why is it a good practice,or not? > Fluent Interfaces – Method Chaining

Appendix: StringBuffer vs StringBuilder

Instead of StringBuffer, you should usually prefer StringBuilder, which is faster because it is out of sync Most of the above discussion also applies to StringBuilder

From document:

Related issues

> StringBuilder and StringBuffer in Java

Bonus materials! Alternatives!

This is another "fix" to the problem and may be more readable:

StringBuilder word = new StringBuilder("murder");
StringBuilder worddrow = new StringBuilder(); // starts empty

worddrow.append(word).append(word.reverse());

System.out.println(worddrow); // "murderredrum"

Note that although this pair of short strings should be OK, it does use additional buffers, which means that it is not the most effective way to solve the problem

Related issues

> Reverse a string in Java,in O(1)? – As charsequence, yes, this can be done!

Bonus materials again! Laugh to the end!

StringBuilder sb = new StringBuilder("ha");
sb.append(sb.append(sb));
System.out.println(sb); // "hahahaha"
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>