Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

[an error occurred while processing this directive]

Java: Summary - java.util.StringBuilder and java.util.StringBuffer

Strings are immutable (can't be changed), so manipulating strings often causes many temporary string objects to be created, which can be quite inefficient. StringBuilder (or StringBuffer) are better choices in these cases. Their toString() method can be used get a String value. It isn't necessary to create them with a specific size, it will expand as necessary. Many StringBuilder methods return the same StringBuilder so that the call chaining style of programming can be used.

StringBuilder is basically identical to the older StringBuffer, but is slightly faster because it isn't synchronized.

Constructors
sb = new StringBuilder() Creates empty StringBuilder
sb = new StringBuffer(n) Creates empty StringBuilder with initial capacity n.
sb = new StringBuffer(s) Creates StringBuilder with value initialized to String s.
Length
i = sb.length() Length of the current contents of sb.
Modifying the content - These methods return the original StingBuilder to allow call chaining.
sb =sb.append(x) Appends x (char, int, String, ...) to end of sb.
sb =sb.insert(offset, x) Inserts x (char, int, String, ...) at position offset.
sb =sb.setCharAt(index, c) Replaces char at index with c
sb =sb.delete(beg, end) Deletes chars at index beg thru end.
sb =sb.reverse() Reverses the contents.
sb =sb.replace(beg, end, s) Replaces characters beg thru end with s.
Getting parts
c = sb.charAt(i) char at position i in sb.
s = sb.substring(i) substring from index i to the end of sb.
s = sb.substring(i, j) substring from index i to BEFORE index j of sb.
Misc
s = sb.toString() Returns a String for the contents of sb.
Searching -- Note: All "indexOf" methods return -1 if the string/char is not found
i = sb.indexOf(s) index of the first occurrence of String s in s.
i = sb.indexOf(s, i) index of String s at or after position i in sb.
i = sb.lastIndexOf(s) index of last occurrence of s in sb.
i = sb.lastIndexOf(s, i) index of last occurrence of s on or before i in sb.
Comparison (note: use this instead of == and !=)
b = sb.equals(sb2) true if the two StringBuffers contain equal values

Copyright 2005 Fred Swartz

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (
post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.