0% found this document useful (0 votes)
7 views6 pages

Java StringBuilder Methods Explained

StringBuilder in Java is a mutable sequence of characters that allows for efficient string modifications through various methods such as append, insert, replace, and delete. Key functionalities include changing string content, managing capacity, and retrieving character information. The document provides code examples for each method to illustrate their usage.

Uploaded by

toui.icyfire
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

Java StringBuilder Methods Explained

StringBuilder in Java is a mutable sequence of characters that allows for efficient string modifications through various methods such as append, insert, replace, and delete. Key functionalities include changing string content, managing capacity, and retrieving character information. The document provides code examples for each method to illustrate their usage.

Uploaded by

toui.icyfire
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

STRING BUILDER IN JAVA

StringBuilder in is a mutable sequence of characters that provides methods for


modifying strings efficiently. Here are some key StringBuilder methods:
1. append()
 Adds text at the end of the StringBuilder object.
Code :
StringBuilder sb = new StringBuilder("Hello");
[Link](" World");
[Link](sb); // Output: Hello World

2. insert()
 Inserts text at a specified position.
Code :
StringBuilder sb = new StringBuilder("Hello");
[Link](5, " Java");
[Link](sb); // Output: Hello Java

3. replace()
 Replaces characters within a specific range.
Code :
StringBuilder sb = new StringBuilder("Hello World");
[Link](6, 11, "Java");
[Link](sb); // Output: Hello Java

4. delete()
 Removes characters within a given range.
Code :
StringBuilder sb = new StringBuilder("Hello World");
[Link](5, 11);
[Link](sb); // Output: Hello

5. deleteCharAt()
 Removes a character at a specified index.
Code :
StringBuilder sb = new StringBuilder("Hello");
[Link](1);
[Link](sb); // Output: Hllo

6. reverse()
 Reverses the characters in the StringBuilder.
Code :
StringBuilder sb = new StringBuilder("Java");
[Link]();
[Link](sb); // Output: avaJ

7. capacity()
 Returns the current capacity of the StringBuilder.
Code :
StringBuilder sb = new StringBuilder();
[Link]([Link]()); // Default: 16

8. ensureCapacity()
 Ensures the capacity is at least a specified value.
Code :
StringBuilder sb = new StringBuilder();
[Link](50);
[Link]([Link]()); // Output: 50 (if initially smaller)

9. charAt()
 Returns the character at a given index.
Code :
StringBuilder sb = new StringBuilder("java");
[Link]([Link](2)); // Output: v

10. length()
 Returns the length of the StringBuilder.
Code :
StringBuilder sb = new StringBuilder("Java");
[Link]([Link]()); // Output: 4

11. setLength()
 Sets the length of the StringBuilder.
Code :
StringBuilder sb = new StringBuilder("Hello");
[Link](3);
[Link](sb); // Output: Hel

12. toString()
 Converts StringBuilder to a String.
Code :
StringBuilder sb = new StringBuilder("java");
String str = [Link]();
[Link](str); // Output:

13. substring()
 Extracts a portion of the string from the StringBuilder.
Code :
StringBuilder sb = new StringBuilder("Hello Java");
[Link]([Link](6)); // Output: Java
[Link]([Link](0, 5)); // Output: Hello

14. setCharAt()
 Modifies a character at a specific index.
Code :
StringBuilder sb = new StringBuilder("Java");
[Link](1, 'O');
[Link](sb); // Output: JOva

15. indexOf()
 Finds the position of the first occurrence of a substring.
Code :
StringBuilder sb = new StringBuilder("Hello Java");
[Link]([Link]("Java")); // Output: 6

16. lastIndexOf()
 Finds the last occurrence of a substring.
Code :
StringBuilder sb = new StringBuilder("Java is Java");
[Link]([Link]("Java")); // Output: 8

17. trimToSize()
 Reduces the capacity to the current length.
Code :
StringBuilder sb = new StringBuilder(50);
[Link]("Java");
[Link]();
[Link]([Link]()); // Output: 4

18. codePointAt()
 Returns the Unicode value of the character at a specified index.
Code :
StringBuilder sb = new StringBuilder("Hello");
[Link]([Link](1)); // Output: 101 (Unicode for 'e')

19. codePointBefore()
 Returns the Unicode value of the character before the specified index.
Code :
StringBuilder sb = new StringBuilder("Hello");
[Link]([Link](1)); // Output: 72 (Unicode for 'H')

20. capacity()
 Returns the current buffer size (not length).
Code :
StringBuilder sb = new StringBuilder();
[Link]([Link]()); // Output: 16 (default)

You might also like