Thursday 10 November 2016

STRING BUFFER



  In Java, StringBuffer class is also used for string handling.
  String creates string of fixed length;StringBuffercreates strings of flexible length that can be modified both in length and content.
  StringBuffer may be declared and created as follows:
StringBuffer str;
str=new StringBuffer(“String”);
Ex:   StringBuffer fname;
         fname=new StringBuffer(“Santosh”);

         StringBuffer fname=new StringBuffer (“Santosh”);
  It is possible to insert character and substrings in the middle of a string, or append another string at the end.
  Some of the common methods used for string manipulation are as follows:
Method
Task
s1.setCharAt (n, ’x’);
Modifies the nth character to ‘x’.
s1.append (s2);
Appends the string s2 to s1 at the end.
s1.insert (n, s2);
Inserts the string s2 at the position n of the string s1.
s1.setLength( n );
If n>s1.length() zeros are added to s1.

No comments:
Write comments