0% found this document useful (0 votes)
61 views3 pages

Java StringBuffer Methods Explained

The java.lang.StringBuffer.ensureCapacity() method ensures that the capacity is at least equal to the specified minimum. If the current capacity is less than the argument, then a new internal array is allocated with greater capacity. The new capacity is the larger of the minimumCapacity argument or twice the old capacity plus 2. The method is demonstrated in examples that show the capacity before and after ensureCapacity() is called.

Uploaded by

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

Java StringBuffer Methods Explained

The java.lang.StringBuffer.ensureCapacity() method ensures that the capacity is at least equal to the specified minimum. If the current capacity is less than the argument, then a new internal array is allocated with greater capacity. The new capacity is the larger of the minimumCapacity argument or twice the old capacity plus 2. The method is demonstrated in examples that show the capacity before and after ensureCapacity() is called.

Uploaded by

soumikbh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

File: Untitled Document 1 Page 1 of 3

public StringBuffer delete(int start, int end)

start − The beginning index, inclusive.

end − The ending index, exclusive.

public class Test {

public static void main(String args[]) {


StringBuffer sb = new StringBuffer("abcdefghijk");
sb.delete(3, 7);
System.out.println(sb);
}

output
abchijk

delete from index(3)=d(included) to index(7)=h(excluded)

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++

The java.lang.StringBuffer.ensureCapacity() method ensures that the capacity is at


least equal to the specified minimum. If the current capacity is less than the
argument, then a new internal array is allocated with greater capacity. The new
capacity is the larger of −

The minimumCapacity argument.


Twice the old capacity, plus 2.

public void ensureCapacity(int minimumCapacity)

class GFG {
public static void main(String[] args)
{

// create a StringBuffer object


StringBuffer str = new StringBuffer();

// print string capacity


System.out.println("Before ensureCapacity "
+ "method capacity = "
+ str.capacity());

// apply ensureCapacity()
str.ensureCapacity(18);

// print string capacity


System.out.println("After ensureCapacity"
+ " method capacity = "
+ str.capacity());
}
}
Output:

Before ensureCapacity method capacity = 16


After ensureCapacity method capacity = 34
File: Untitled Document 1 Page 2 of 3

Example 2:
filter_none

edit

play_arrow

brightness_4
// Java program to demonstrate
// the ensureCapacity() Method.

class GFG {
public static void main(String[] args)
{

// create a StringBuffer object


StringBuffer
str
= new StringBuffer("Geeks For Geeks");

// print string capacity


System.out.println("Before ensureCapacity "
+ "method capacity = "
+ str.capacity());

// apply ensureCapacity()
str.ensureCapacity(42);

// print string capacity


System.out.println("After ensureCapacity"
+ " method capacity = "
+ str.capacity());
}
}
Output:

Before ensureCapacity method capacity = 31


After ensureCapacity method capacity = 64

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

The java lang.string.trim()is a built-in function that eliminates leading and


trailing spaces.

str=" SKFGI "


str.trim() returns "SKFGI"
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

String substring() : This method has two variants and returns a new string that is
a substring of this string. The substring begins with the character at the
specified index and extends to the end of this string.

Syntax :
public String substring(int begIndex)
Parameters :
begIndex : the begin index, inclusive.
File: Untitled Document 1 Page 3 of 3

Return Value :
The specified substring.

ANOTHER process

public String substring(int begIndex, int endIndex)


Parameters :
beginIndex : the begin index, inclusive.
endIndex : the end index, exclusive.
Return Value :
The specified substring.

examples:
String Str = new String("Welcome to MAKAUT");
System.out.print("The extracted substring is : ");
System.out.println(Str.substring(10));
output
" MAKAUT"

String Str = new String("Welcome to geeksforgeeks");


System.out.print("The extracted substring is : ");
System.out.println(Str.substring(10, 13));

output
" MA"

You might also like