Example : A Java program to find the length of string using Stringbuffer class method. [ length() ]
class Example
{
public static void main(String args[])
{
int len;
StringBuffer sb=new StringBuffer("Codershelpline");
len=sb.length();
System.out.println(len);
}
}
Output :
14
Example : A Java program to find/display character at the specified position in given string using Stringbuffer class method. [ charAt() ]
class Example
{
public static void main(String args[])
{
char ch;
StringBuffer sb=new StringBuffer("Codershelpline");
ch=sb.charAt(3);
System.out.println(ch);
}
}
Output :
e
Example : A Java program to find the substring of a given string from specified beginning Index using Stringbuffer class method. [ substring() ]
class Example
{
public static void main(String args[])
{
String Str;
StringBuffer sb=new StringBuffer("Codershelpline");
Str=sb.substring(6);
System.out.println(Str);
}
}
Output :
helpline
---------- OR ---------
class Example
{
public static void main(String args[])
{
String Str;
StringBuffer sb=new StringBuffer("Codershelpline");
Str=sb.substring(0,6);
System.out.println(Str);
}
}
Output :
Coders
---------- OR ---------
class Example
{
public static void main(String args[])
{
String Str;
StringBuffer sb=new StringBuffer("Codershelpline");
Str=sb.substring(3,7);
System.out.println(Str);
}
}
Output :
ersh
Example : A Java program to concatenate string using Stringbuffer class method. [ append() ]
class Example
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("Coders ");
sb.append("Helpline");
System.out.println(sb);
}
}
Output :
Coders Helpline
Example : A Java program to add new string at specific position in a given string using Stringbuffer class method. [ insert() ]
class Example
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("Coders");
sb.insert(2,"Helpline");
System.out.println(sb);
}
}
Output :
CoHelplineders
Example : A Java program to replace old string character from new string at specific position using Stringbuffer class method. [ replace() ]
class Example
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("Coders");
sb.replace(2,5,"Helpline");
System.out.println(sb);
}
}
Output :
CoHelplines
Example : A Java program to remove/delete certain string character from a given string at specific position using Stringbuffer class method. [ delete() ]
class Example
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("Codershelpline");
sb.delete(2,5);
System.out.println(sb);
}
}
Output :
Coshelpline
Example : A Java program to reverse the given string using Stringbuffer class method. [ reverse() ]
class Example
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("CodersHelpline");
sb.reverse();
System.out.println(sb);
}
}
Output :
enilpleHsredoC
Example : A Java program to check the buffer size of the given string using Stringbuffer class method. [ capacity() ]
class Example
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer();
System.out.println(sb.capacity()); //Default Buffer Size = 16
sb.append("Codershelpline");
System.out.println(sb.capacity()); //Current Buffer Size = 16
sb.append("Codershelpline is a simple site for students");
System.out.println(sb.capacity()); //Current Buffer Size = 58
sb.append("Codershelpline is a simple site for students.It is best for tutorial as
well as project developing and programming languages");
System.out.println(sb.capacity()); //Current Buffer Size = 182
}
}
Output :
16
16
58
182
NB:
This function increases the default buffer capacity(16) of the buffer to store the more string from its current default capacity, it increases the capacity by (oldcapacity*2)+2. For example if our current capacity is 16, it will be (16*2)+2=34.
Example : A Java program to find the length of string using Stringbuilder class method. [ length() ]
class Example
{
public static void main(String args[])
{
int len;
StringBuilder sb=new StringBuilder("Codershelpline");
len=sb.length();
System.out.println(len);
}
}
Output :
14
NB : StringBuilder class conatains almost similar functions, in syntax and definition, of StringBuffer class except classname.
0 Comments