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

How to use StringBuffer

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

Share

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

This article mainly introduces "the use of StringBuffer". In the daily operation, I believe that many people have doubts about the use of StringBuffer. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about the use of StringBuffer. Next, please follow the editor to study!

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)

The value of the object sb becomes "abctrue".

Using this method to concatenate strings will save more content than String, such as connections applied to database SQL statements, such as:

StringBuffer sb = new StringBuffer ()

String user = "test"

String pwd = "123"

Sb.append ("select * from userInfo where username=")

.append (user)

.append ("and pwd=")

.append (pwd)

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".

E, setCharAt method

Public void setCharAt (int index, char ch)

The purpose of this method is to change the character in the object whose index value is index position to the new character ch. For example:

StringBuffer sb = new StringBuffer ("abc")

Sb.setCharAt (1)

The value of the object sb becomes "aDc".

F, trimToSize method

Public void trimToSize ()

The function of this method is to reduce the storage space in the StringBuffer object to the same length as the string length and reduce the waste of space.

In a word, in actual use, String and StringBuffer have their own advantages and disadvantages, so we can choose the corresponding type to use according to the specific use environment.

At this point, the study of "how to use StringBuffer" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Internet Technology

Wechat

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

12
Report