![]() |
||
![]() |
![]() Alden Hosting provides professional, efficient, and reliable business-class Web hosting services to small- and medium-sized businesses. |
|
JAVA, JSP, SERVLETS, TOMCAT, SERVLETS MANAGER, |
Constructor | Description |
---|---|
StringBuilder()
|
Creates an empty string builder with a capacity of 16 (16 empty elements). |
StringBuilder(CharSequence cs)
|
Constructs a string builder containing the same characters as the
specified CharSequence , plus an extra 16 empty elements
trailing the CharSequence .
|
StringBuilder(int initCapacity)
|
Creates an empty string builder with the specified initial capacity. |
StringBuilder(String s)
|
Creates a string builder whose value is initialized by the specified string, plus an extra 16 empty elements trailing the string. |
For example, the following code
will produce a string builder with a length of 9 and a capacity of 16:StringBuilder sb = new StringBuilder(); // creates empty builder, capacity 16 sb.append("Greetings"); // adds 9 character string at beginning
The StringBuilder
class has some methods related to length and capacity that the
String
class does not have:
Method | Description |
---|---|
void setLength(int newLength)
|
Sets the length of the character sequence. If newLength is less
than length() , the last characters in the character sequence are
truncated. If newLength is greater than length() ,
null characters are added at the end of the character sequence.
|
void ensureCapacity(int minCapacity)
|
Ensures that the capacity is at least equal to the specified minimum. |
A number of operations (for example, append()
, insert()
, or setLength()
)
can increase the length of the character sequence in the string builder so that
the resultant length()
would be greater than the current capacity()
.
When this happens, the capacity is automatically increased.
StringBuilder
that are not available in String
are the
append()
and insert()
methods, which
are overloaded so as to accept data of any type. Each converts its argument
to a string and then appends or inserts the characters of that string to the character sequence
in the string builder. The append method always adds these characters at the end of the existing character sequence,
while the insert method adds the characters at a specified point.
Here are a number of the methods of the StringBuilder
class.
Method | Description |
---|---|
StringBuilder append(boolean b)
|
Appends the argument to this string builder. The data is converted to a string before the append operation takes place. |
StringBuilder delete(int start, int end)
|
Deletes the specified character(s) in this string builder. |
StringBuilder insert(int offset, boolean b)
|
Inserts the second argument into the string builder. The first integer argument indicates the index before which the data is to be inserted. The data is converted to a string before the insert operation takes place. |
StringBuilder replace(int start, int end, String s)
|
Replaces the specified character(s) in this string builder. |
StringBuilder reverse()
|
Reverses the sequence of characters in this string builder. |
String toString()
|
Returns a string that contains the character sequence in the builder. |
Note: You can use anyString
method on aStringBuilder
object by first converting the string builder to a string with thetoString()
method of theStringBuilder
class. Then convert the string back into a string builder using theStringBuilder(String str)
constructor.
StringDemo
program that was listed in the section titled "Strings" is an example of
a program that would be more efficient if a StringBuilder
were used instead of a String
.
StringDemo
reversed a palindrome. Here, once again, is its listing:
Running the program produces this output:public class StringDemo { public static void main(String[] args) { String palindrome = "Dot saw I was Tod"; int len = palindrome.length(); char[] tempCharArray = new char[len]; char[] charArray = new char[len]; // put original string in an array of chars for (int i = 0; i < len; i++) { tempCharArray[i] = palindrome.charAt(i); } // reverse array of chars for (int j = 0; j < len; j++) { charArray[j] = tempCharArray[len - 1 - j]; } String reversePalindrome = new String(charArray); System.out.println(reversePalindrome); } }
To accomplish the string reversal, the program converts the string to an array of characters (firstdoT saw I was toD
for
loop),
reverses the array into a second array (second for
loop), and then converts back to a string.
If you convert the palindrome
string to a string builder, you can use the reverse()
method in the StringBuilder
class. It makes the code simpler and easier to read:
Running this program produces the same output:public class StringBuilderDemo { public static void main(String[] args) { String palindrome = "Dot saw I was Tod"; StringBuilder sb = new StringBuilder(palindrome); sb.reverse(); // reverse it System.out.println(sb); } }
Note thatdoT saw I was toD
println()
prints a string builder, as in:
becauseSystem.out.println(sb);
sb.toString()
is called implicitly, as it is with any other object in a
println()
invocation.
Note: There is also aStringBuffer
class that is exactly the same as theStringBuilder
class, except that it is thread-safe by virtue of having its methods synchronized. Threads will be discussed in the lesson on concurrency.
Alden Hosting offers private JVM (Java Virtual Machine), Java Server Pages (JSP), Servlets, and Servlets Manager with our Web Hosting Plans WEB 4 PLAN and WEB 5 PLAN , WEB 6 PLAN .
At Alden Hosting we eat and breathe Java! We are the industry leader in providing affordable, quality and efficient Java web hosting in the shared hosting marketplace. All our sites run on our Java hosing platform configured for optimum performance using Java, Tomcat, MySQL, Apache and web application frameworks such as Struts, Hibernate, Cocoon, Ant, etc.
We offer only one type of Java hosting - Private Tomcat. Hosting accounts on the Private Tomcat environment get their very own Tomcat server. You can start and re-start your entire Tomcat server yourself.
![]() |
|
http://alden-servlet-Hosting.com
JSP at alden-servlet-Hosting.com
Servlets at alden-servlet-Hosting.com
Servlet at alden-servlet-Hosting.com
Tomcat at alden-servlet-Hosting.com
MySQL at alden-servlet-Hosting.com
Java at alden-servlet-Hosting.com
sFTP at alden-servlet-Hosting.com
http://alden-tomcat-Hosting.com
JSP at alden-tomcat-Hosting.com
Servlets at alden-tomcat-Hosting.com
Servlet at alden-tomcat-Hosting.com
Tomcat at alden-tomcat-Hosting.com
MySQL at alden-tomcat-Hosting.com
Java at alden-tomcat-Hosting.com
sFTP at alden-tomcat-Hosting.com
http://alden-sftp-Hosting.com
JSP at alden-sftp-Hosting.com
Servlets at alden-sftp-Hosting.com
Servlet at alden-sftp-Hosting.com
Tomcat at alden-sftp-Hosting.com
MySQL at alden-sftp-Hosting.com
Java at alden-sftp-Hosting.com
sFTP at alden-sftp-Hosting.com
http://alden-jsp-Hosting.com
JSP at alden-jsp-Hosting.com
Servlets at alden-jsp-Hosting.com
Servlet at alden-jsp-Hosting.com
Tomcat at alden-jsp-Hosting.com
MySQL at alden-jsp-Hosting.com
Java at alden-jsp-Hosting.com
sFTP at alden-jsp-Hosting.com
http://alden-java-Hosting.com
JSp at alden-java-Hosting.com
Servlets at alden-java-Hosting.com
Servlet at alden-java-Hosting.com
Tomcat at alden-java-Hosting.com
MySQL at alden-java-Hosting.com
Java at alden-java-Hosting.com
sFTP at alden-java-Hosting.com
JSP
Servlets
Tomcat
mysql
Java
JSP
Servlets
Tomcat
mysql
Java
JSP
Servlets
Tomcat
mysql
Java
JSP
Servlets
Tomcat
mysql
Java
JSP at JSP.aldenWEBhosting.com
Servlets at servlets.aldenWEBhosting.com
Tomcat at Tomcat.aldenWEBhosting.com
mysql at mysql.aldenWEBhosting.com
Java at Java.aldenWEBhosting.com
Web Hosts Portal
Web Links
Web Links
Web Hosting
JSP Solutions Web Links
JSP Solutions Web Hosting
Servlets Solutions Web Links
Servlets Solutions Web Hosting
Web Links
Web Links
.
.
.
.
.
.
.
.
.
.
jsp hosting
servlets hosting
web hosting
web sites designed
cheap web hosting
web site hosting
myspace web hosting