Java replace spaces

Please implement a function to replace spaces in a string with "% 20". For example, when the string is we are happy Then the replaced string is we% 20are% 20happy.

The following is Java lang.StringBuilder. Declaration of the replace() method

public StringBuilder replace(int start,int end,String str)

parameter

String constant

StringBuffer string variable (thread safe)

StringBuilder string variable (non thread safe)

Briefly speaking, the main performance difference between string type and StringBuffer type is that string is an immutable object. Therefore, every time the string type is changed, it is equivalent to generating a new string object, and then pointing the pointer to the new string object. Therefore, it is better not to use string for strings that often change content, Because each generation of objects will have an impact on the system performance, especially when there are many non reference objects in memory, the GC of the JVM will start to work, and the speed must be quite slow.

If the StringBuffer class is used, the result will be different. Each result will operate on the StringBuffer object itself instead of generating a new object and changing the object reference. Therefore, in general, we recommend using StringBuffer, especially when string objects change frequently. In some special cases, the string splicing of string objects is actually interpreted by the JVM as the splicing of StringBuffer objects, so the speed of string objects is not slower than that of StringBuffer objects. In particular, in the generation of the following string objects, the string efficiency is much faster than that of StringBuffer:

You will be surprised to find that the speed of generating string S1 objects is too fast, and at this time, StringBuffer does not have an advantage at all. In fact, this is a trick of the JVM. In the eyes of the JVM, this

String S1 = “This is only a” + “ simple” + “test”; In fact:

String S1 = “This is only a simple test”; So of course it doesn't take much time. However, it should be noted here that if your string comes from another string object, the speed will not be so fast, for example:

At this time, the JVM will behave in the original way

In most cases, StringBuffer > string

StringBuffer

Java. Lang.stringbuffer thread safe variable character sequence. A string buffer similar to string, but cannot be modified. Although it contains a specific character sequence at any point in time, the length and content of the sequence can be changed through some method calls.

String buffers can be safely used for multiple threads. These methods can be synchronized when necessary, so all operations on any particular instance appear to occur in a serial order, which is consistent with the order of method calls made by each thread involved.

The main operations on StringBuffer are append and insert methods, which can be overloaded to accept any type of data. Each method can effectively convert the given data into a string, and then append or insert the characters of the string into the string buffer. The append method always adds these characters to the end of the buffer; The insert method adds characters at the specified point.

For example, if Z refers to a string buffer object whose current content is "start", this method calls z.append ("Le") to make the string buffer contain "start", while z.insert (4, "Le") will change the string buffer to contain "starret".

In most cases, StringBuilder > StringBuffer

java. lang.StringBuilde

java. Lang. StringBuilder a variable character sequence is new in 5.0. This class provides an API compatible with StringBuffer, but does not guarantee synchronization. This class is designed as a simple alternative to StringBuffer when the string buffer is used by a single thread (which is common). If possible, it is recommended to take this class first because it is faster than StringBuffer in most implementations. The methods of the two are basically the same.

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

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
分享
二维码
< <上一篇
下一篇>>