Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the uses of JAVA StringBuffer?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

What is the use of JAVA StringBuffer, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Like String, StringBuffer class is also used to represent strings, but because the internal implementation of StringBuffer is different from that of String, StringBuffer does not generate new objects when dealing with strings, so it is better than String class in memory usage.

1. Initialization of StringBuffer object

The initialization of StringBuffer objects is not like the initialization of the String class. Java provides a special syntax, while constructors are usually used for initialization.

For example:

StringBuffer s = new StringBuffer ()

The StringBuffer object initialized in this way is an empty object, which is my mistake.

If you need to create a StringBuffer object with content, you can use:

StringBuffer s = new StringBuffer ("abc")

The content of the initialized StringBuffer object is the string "abc".

It is important to note that StringBuffer and String are of different types and cannot be cast directly, and the following code is incorrect:

StringBuffer s = "abc"; / / assignment types do not match

StringBuffer s = (StringBuffer) "abc"; / / No inheritance relationship exists and cannot be forcibly transferred

The code for the conversion between the StringBuffer object and the String object is as follows:

String s = "abc"

StringBuffer sb1 = new StringBuffer ("123")

StringBuffer sb2 = new StringBuffer (s); / / convert String to StringBuffer

String S1 = sb1.toString (); / / convert StringBuffer to String

2. Common methods of StringBuffer

The methods in the StringBuffer class mainly focus on string changes, such as appends, inserts, and deletions, which is also the main difference between the StringBuffer and String classes.

A, append method

Public StringBuffer append (boolean b)

The purpose of this method is to append the content to the end of the current StringBuffer object, similar to a string concatenation. After the method is called, the content of the StringBuffer object also changes, for example:

StringBuffer sb = new StringBuffer ("abc")

Sb.append (true)

So the value of the object sb is the string "select * from userInfo where username=test and pwd=123".

B, deleteCharAt method

Public StringBuffer deleteCharAt (int index)

The purpose of this method is to delete the characters at the specified location and then form the rest into a new string. For example:

StringBuffer sb = new StringBuffer ("Test")

Sb. DeleteCharAt (1)

The purpose of this code is to delete the character with an index value of 1 in the string object sb, that is, to delete the second character, and the rest to form a new string. So the value of the object sb becomes "Tst".

There is also a similar delete method:

Public StringBuffer delete (int start,int end)

The purpose of this method is to delete all characters within the specified interval, including start, and the interval that does not contain the end index value. For example:

StringBuffer sb = new StringBuffer ("TestString")

Sb. Delete (1pr 4)

The purpose of this code is to delete all characters from index value 1 (inclusive) to index value 4 (excluding), and the remaining characters form a new string. The value of the object sb is "TString".

C, insert method

Public StringBuffer insert (int offset, boolean b)

The purpose of this method is to insert content into the StringBuffer object and then form a new string. For example:

StringBuffer sb = new StringBuffer ("TestString")

Sb.insert (4 false)

The purpose of the sample code is to insert the false value at the index value 4 of the object sb to form a new string, then the value of the object sb is "TestfalseString" after execution.

D, reverse method

Public StringBuffer reverse ()

The purpose of this method is to reverse the contents of the StringBuffer object and form a new string. For example:

StringBuffer sb = new StringBuffer ("abc")

Sb.reverse ()

After reversal, the content in the object sb changes to "cba".

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report