Java replaces Spaces
- 2020-06-01 09:45:27
- OfStack
Please implement a function that replaces the space in a string with "%20". For example, when the string is We Are Happy. The replaced string is We%20Are%20Happy.
public class Solution {
public String replaceSpace(StringBuffer str) {
for(int k=0;k<str.length();k++)
{
char index=str.charAt(k);
if(index==' ')
{
str.replace(k,k+1,"%20");
}
}
return str.toString();
}
}
. The following is a java lang. StringBuilder. replace () method statement
public StringBuilder replace(int start, int end, String str)
parameter
start - this is the start index (included). end -- end index (not included). str -- this is the string that will replace the previous content.String string constants
StringBuffer string variable (thread safe)
StringBuilder string variable (non-thread safe)
In short, String type and the main performance difference is actually String StringBuffer types are immutable object, so in every time to change String types are equivalent to generate a new String object, then the pointer to the new String object, so often it is best not to change the contents of the string with String, because every time generated objects will have an effect on system performance, especially when memory without much reference object, JVM's GC is going to start working, so it's going to be pretty slow at 1.
If you use the StringBuffer class, the result will be different. Each time the result will operate on the StringBuffer object itself, instead of generating a new object and changing the object reference. So we recommend StringBuffer in the 1 case, especially if the string object changes frequently. In some special cases, the string concatenation of String objects is actually interpreted as the concatenation of StringBuffer objects by JVM, so the speed of String objects is not slower than that of StringBuffer objects at these times, and String's efficiency is much faster than that of StringBuffer objects in the generation of the following string objects:
String S1 = " This is only a " + " simple " + " test " ;
StringBuffer Sb = new StringBuilder( " This is only a " ).append( " simple " ).append( " test " );
You will be surprised to find that String S1 objects are generated so quickly that StringBuffer is not even 1 point faster. This is actually a trick of JVM, in JVM's eyes, this
String S1 = "This is only a" + "simple" + "test"; It is:
String S1 = "This is only a simple test"; So of course it doesn't take much time. But the thing to note here is that if your string comes from another String object, it's not as fast. For example:
String S2 = " This is only a " ;
String S3 = " simple " ;
String S4 = " test " ;
String S1 = S2 +S3 + S4;
At this point JVM will do it the way it was done
In most cases StringBuffer > String
StringBuffer
Java.lang.StringBuffer thread-safe mutable character sequences. A string buffer similar to String, but cannot be modified. Although it contains a particular sequence of characters at any point in time, the length and content of that sequence can be changed by certain method calls.
The string buffer can be used safely for multiple threads. These methods can be synchronized as necessary, so all operations on any particular instance appear to occur in a serial order, one in the order of method calls made by each of the threads involved.
The main operations on StringBuffer are the append and insert methods, which can be overloaded to accept any type of data. Each method effectively converts the given data into a string, and then appends or inserts the characters of that 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 references a string buffer object whose current content is "start", this method calls z.append ("le") to make the string buffer contain "startle", and z.insert (4, "le") to change the string buffer to include "starlet".
In most cases StringBuilder > StringBuffer
java.lang.StringBuilde
java.lang.StringBuilder1 a variable character sequence is new to 5.0. This class provides an API compatible with StringBuffer, but does not guarantee synchronization. This class is designed to be a simple replacement for StringBuffer when (as is common) the string buffer is used by a single thread. If possible, it is recommended that this class be preferred, as it is faster than StringBuffer in most implementations. The methods are basically the same.