Java string buffer

Java string buffer

what is stringBuffer and StringBuilder?
View Answers

October 30, 2010 at 1:00 PM

Hi friend,

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.

StringBuilder

import javax.swing.*;

public class ReverseExample { public static void main(String[] args) { String input; // Used for the input string. String reversed; // Reversed form or the input string.

while(true){ input = JOptionPane.showInputDialog(null, "Enter a string"); if(input == null) break; reversed = ""; for(int i=0; i

StringBuffer classes

import java.io.*;

public class stringBufferExam{ public static void main(String[] args) throws Exception{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String str; try{ System.out.print("Enter your name: "); str = in.readLine(); System.out.println("This is the example of SringBuffer class and it's functions.");

//Create a object of StringBuffer class StringBuffer buff = new StringBuffer(); buff.append(str); System.out.println(buff); buff.delete(0,str.length());

//append() buff.append("Hello"); buff.append("World"); //print HelloWorld System.out.println(buff);

//insert() buff.insert(5,"Java "); //print HelloJava World System.out.println(buff);

//reverse() buff.reverse(); System.out.print("Reversed string : "); System.out.println(buff); //print dlroW avaJ_olleH buff.reverse(); System.out.println(buff); //print Hello_Java World

} catch(StringIndexOutOfBoundsException e){ System.out.println(e.getMessage()); } }

}

read for more information,

http://www.roseindia.net/java/beginners/StringBuffer.shtml

thanks,









Related Tutorials/Questions & Answers:
Java string buffer
Java string buffer  what is stringBuffer and StringBuilder
Java string buffer
Java string buffer  What is the basic difference between string and stringbuffer object
Advertisements
(ii) String and String Buffer Class
(ii) String and String Buffer Class  difference between String and String Buffer Class
(ii) String and String Buffer Class
String and String Buffer Class  differences between : String and String Buffer Class
(ii) String and String Buffer Class
String and String Buffer Class  difference between String and String Buffer Class... whereas String buffer objects are not constants but growable
explain the difference between string builder and string buffer classes
explain the difference between string builder and string buffer classes  Hi, Here is my code public static void main(String[] args) { String s1 = "abc"; StringBuffer s2 = new StringBuffer(s1); StringBuffer s3 = s2
Java code for buffer operation...!!!
Java code for buffer operation...!!!  Implement a buffer operation... BufferOperation { public static void main(String[] args) { StringBuffer buffer=new StringBuffer(); Scanner input=new Scanner(System.in
How to create a duplicate buffer of a long buffer in java.
How to create a duplicate buffer of a long buffer in java. In this tutorial, we will see how to create a duplicate buffer that shares the content of long... C:\>java DuplicateBuffer Content in original long buffer
How to clear a float buffer in java.
How to clear a float buffer in java. In this tutorial, we will  see how to clear a float buffer in java. ByteBuffer API: The java.nio.FloatBuffer class... a new float buffer of given capacity.  abstract FloatBuffer
Java string
Java string  Strings are immutable.But String s="Hello"; String s1=s+"World"; S.O.P(s1); means printing "Hello World". How
String Mirror in Java
String Mirror in Java  how to get a mirror image of(string) a given string in java   Hello Friend, Try this:ADS_TO_REPLACE_1 import java.util.*; class StringMirror{ public static void main(String []args){ String str
How to rewind float buffer in java.
How to rewind float buffer in java. In this tutorial, we will see how to rewind float buffer in java. In this example, The rewind method reset...:\r>java RewindFloatBuffer Data in float buffer. 4.8 3.9 5.6 After
How to clear int buffer in java.
How to clear int buffer in java. In this tutorial, we will discuss how to clear int buffer in java.          ...;} } Output C:\>java IntBufferClear Int value in the buffer. 22
How to allocate a short buffer in java.
How to allocate a short buffer in java.  In this tutorial, we will see how to allocate a new short buffer. ShortBuffer API: The java.nio.ShortBuffer... allocate a short buffer of given capacity.  Buffer API
How to allocate a new int buffer in java.
How to allocate a new int buffer in java.  In this tutorial, we will see how to allocate a new int buffer. IntBuffer API: The java.nio.IntBuffer... a int buffer of given capacity.  Buffer API
How to allocate a new long buffer in java.
How to allocate a new long buffer in java.  In this tutorial, we will see how to allocate a new long buffer. LongBuffer API: The java.nio.LongBuffer... allocate a long buffer of given capacity.  Buffer API
String and StringBuffer - Java Beginners
; Hi vijay Java provides the StringBuffer and String classes... buffer implements a mutable sequence of characters. A string buffer is like...String and StringBuffer  Dear Deepak Sir, What is String
How to allocate a float buffer in java.
How to allocate a float buffer in java.  In this tutorial, we will see how to allocate a new float buffer. FloatBuffer API: The java.nio.FloatBuffer... allocate a float buffer of given capacity.  int limit
please clarify regarding capacity method of my below string buffer class example
please clarify regarding capacity method of my below string buffer class example  hi here is the program below: > import java.lang.*; public class > Compare{ public static void > main(String[] args) { >
Java String
Java String  You are given two strings S1 and S2,Delete from S2 all those characters which occur in s1 also and create a clean S2 with the relevant characters deleted..write a pgm for this..pls send as soon as possible Thank you
How to rewind the int buffer in java.
How to rewind the int buffer in java. In this tutorial, we will discuss how to rewind the int buffer in java.  IntBuffer API... create a int buffer of specified capacity.  abstract
How to create a read-only long buffer in java.
How to create a read-only long buffer in java. In this tutorial, we will see how to create a read-only long buffer that shares the content of another long... C:\>Java ReadOnlyBuffer Content in long buffer. 345678 8765433
ShortBuffer in java, How to rewind a short buffer in java.
ShortBuffer in java, How to rewind a short buffer in java. In this tutorial, we will see how to rewind a short buffer in java. ShortBuffer API...;java RewindShortBuff Elements in short buffer : 12 21 87 34 12 After using
Java String
Java String       In this section, you will learn about the java string. Java String is a set... to a string literal in the object stream. The Java String object contains a sequence
Java String operations
Java String operations  hi, please suggest me how to write the java...*; class ReadFileIgnoringSomeLines { public static void main(String[] args..."); BufferedReader br=new BufferedReader(fr); StringBuffer buffer=new
string - Java Beginners
string in java interview questions  What is string in java? Interview questions
How to check long buffer is direct or not in java.
How to check long buffer is direct or not in java. In this tutorial, we will discuss how to check long buffer is direct or not in java. LongBuffer API...() method create a long buffer of specified capacity. 
capitalizing string in java
capitalizing string in java  How to capitalizing string in Java?   String s = "some string"; String capitol = Character.toString(s.charAt(0)).toUpperCase(); String newString = capitol + s.substring(1,x
How to clean a buffer using clear method in java.
How to clean a buffer using clear method in java.      ... in Buffer class.The ByteBuffer class is available in java.nio package...;    In this example, the clear() method clean a buffer for further
How to split string in Java?
How to split string in Java?  Hi, I have a string seperated by ',' comma. How to split the string? Thanks
String immutable - Java Beginners
String immutable  Why is String immutable?  See here Why String is immutable in Java
How to Append String In Java?
How to Append String In Java?  How to write program in in Java for appending to a String? How to Append String In Java? What is the best way to do... the StringBuilder class. Check the example at Appending String in java. You can
converting string to double in java
converting string to double in java  Please post an example to converting string to double in java. Thanks!   Convert String to Double Tutorial
ShortBuffer in java, Compare a short buffer with another short buffer.
;} } Output C:\>java CompareToShortBuffer This short buffer...Compare a short buffer with another short buffer.  In this tutorial, we will see how to compare a short buffer with another short buffer
Java String Datatype
Java String Datatype  Weather String in java is class or Datatype,if it's clss then How can i assign String str="hello";   String is a class found in the package java.lang.*. When we represent string in double quotes
How to convert into to String in Java?
How to convert into to String in Java?  Hi, How to convert into to String in Java? Thanks
How To Append String In Java
How To Append String In Java In this section we will read about how to append String in Java. Here we will see the solution of this question through a simple example. There are various ways to append String in Java. Below, we
Java String - Java Beginners
Java String  How to seperate the string into characters.  there is a method called getChars() in String class,plz use that and separate into characters so plz verify the String api for related questions 
Appending String efficiently in Java
for Appending String efficiently in Java. Thanks   Hi, StringBuilder class of Java API is used by developers of Java for appending string... example at How To Append String In Java? Thanks
Core Java String
Core Java String  Hi , Here my code Class Test { String s1 = "null"; String s2 = "null"; String s3 = s1+s2; } what is the out put
On string - Java Beginners
On string -Display reverse String using string reverse method in Java  I wanted to display reverse String using string.reverse() method in Java  Display reverse String using string.reverse()String Reverse Using
java programmming: string manipulation
java programmming: string manipulation  WAP to accept a string from...' occurring in it   Here is a java example that reads a string from the command prompt and find the frequency of the character e in the input string
How to append String in Java?
How to append String in Java?  Hi, I have a number of string in Java which is to be appended efficiently. How to append String in Java? Thanks...: str.append("India"); You check complete tutorial at Appending String in Java
String in Java - Java Beginners
]); } } } ---------------------------------------------------- I am sending simple code of String array. If you are beginner in java...String in Java  hiiiii i initialise a String array,str[] with a size... in advance   Hello Antony Insted Using String Array Use StringBuffer
java string vowel program
java string vowel program  java string vowel program   Please visit the following links: Display the word that begins with vowel Java Count Vowels
java string multiplication
java string multiplication  Is there any simple method of string multiplication in java ..?   public class StringMultiplication { public static void main(String[] args) { String number1 = "17"; String number2
Java String searching.
Java String searching.  How to search a string in given String array... static void main(String[] args) { String city[] = { "Delhi", "Lucknow", "Mumbai", "Kanpur" }; String userCity; Scanner scanner = new
string
string   difference detween "public static void main (String[] args) " and "public static void main (String args[])" in java but it executes both ,in java which one is legal   The only difference is style, where
java string comparison example
java string comparison example  how to use equals method in String... static void main(String [] args){ String str="Rose"; String str1... using equals() method. In the above example, we have declared two string
String - Java Interview Questions
Java String array declaration and initialization  I am looking for an example of java string array declaration and initialization  Hipublic class ImmutableCart { private final List items; public ImmutableCart(List

Ads