Java — Important Methods of StringBuffer Class | Code Factory

StringBuffer sb = new StringBuffer("Code");
System.out.println(sb.length()); // 4
StringBuffer sb = new StringBuffer("Code");
System.out.println(sb.capacity()); // 20
StringBuffer sb = new StringBuffer("Code");
System.out.println(sb.charAt(0)); // C
System.out.println(sb.charAt(5)); // RE : java.lang.StringIndexOutOfBoundsException: String index out of range: 5
StringBuffer sb = new StringBuffer("Code");
sb.setCharAt(3, 'o');
System.out.println(sb); // Codo
1. public StringBuffer append(boolean b)
2. public StringBuffer append(char c)
3. public StringBuffer append(char[] str)
4. public StringBuffer append(char[] str, int offset, int len)
5. public StringBuffer append(CharSequence s)
6. public StringBuffer append(CharSequence s, int start, int end)
7. public StringBuffer append(double d)
8. public StringBuffer append(float f)
9. public StringBuffer append(int i)
10. public StringBuffer append(long lng)
11. public StringBuffer append(Object obj)
12. public StringBuffer append(String str)
13. public StringBuffer append(StringBuffer sb)
StringBuffer sb = new StringBuffer("Code");
sb.append(123);
System.out.println(sb); // Code123
1. public StringBuffer insert(int offset, boolean b)
2. public StringBuffer insert(int offset, char c)
3. public StringBuffer insert(int offset, char[] str)
4. public StringBuffer insert(int index, char[] str, int offset, int len)
5. public StringBuffer insert(int dstOffset, CharSequence s)
6. public StringBuffer insert(int dstOffset, CharSequence s, int start, int end)
7. public StringBuffer insert(int offset, double d)
8. public StringBuffer insert(int offset, float f)
9. public StringBuffer insert(int offset, int i)
10. public StringBuffer insert(int offset, long l)
11. public StringBuffer insert(int offset, Object obj)
12. public StringBuffer insert(int offset, String str)
StringBuffer sb = new StringBuffer("Code");
sb.insert(0, 123);
System.out.println(sb); // 123Code
StringBuffer sb = new StringBuffer("Code");
sb.delete(3, 4);
System.out.println(sb); // Cod
StringBuffer sb = new StringBuffer("Code");
sb.deleteCharAt(3);
System.out.println(sb); // Cod
StringBuffer sb = new StringBuffer("Code");
System.out.println(sb.reverse()); // edoC
StringBuffer sb = new StringBuffer("CodeFactory");
sb.setLength(4);
System.out.println(sb); // Code
StringBuffer sb = new StringBuffer();
System.out.println(sb.capacity()); // 16
sb.ensureCapacity(50);
System.out.println(sb.capacity()); // 50
StringBuffer sb = new StringBuffer(50);
sb.append("Code");
System.out.println(sb.capacity()); // 50
sb.trimToSize();
System.out.println(sb.capacity()); // 4

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Deploying a Java Web Application in Apache Tomcat App Server.

How to do clustering with Google Maps in iOS

Wroclove.rb 2019 highlights for Ruby developers- datarockets

The Quickest & Easiest Way To MOBILE AUTOMATION

Things To Keep In Mind While Developing A Messaging App

Fancy picker: story of one background

Connecting the dots — Big O and Array Data Structure

A Real-World Example of Go Interfaces

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Topics for Interview: OOPS, C, C++

What is the SOLID principle?

Factory Method Design Pattern

Polymorphism in C# with Method Overloading And Method Overriding