Programming Articles - Page 2926 of 3363

Reverse the sequence of characters in a StringBuffer Object in Java

Anvi Jain
Updated on 26-Jun-2020 14:57:30

232 Views

In order to reverse the sequence of characters in a StringBuffer Object, we use the reverse() method. The reverse() method replaces the character sequence with the reverse of its own.Declaration − The java.lang.StringBuffer.reverse method is declared as follows−public StringBuffer reverse()Let us see a program to reverse the sequence of characters in a StringBuffer Object using the reverse() method.Example Live Demopublic class Example {    public static void main(String[] args) {       StringBuffer sb = new StringBuffer("Hello World");       System.out.println("Original StringBuffer Object: " + sb);       sb.reverse();       System.out.println("New StringBuffer Object: " +sb);   ... Read More

Remove substring from StringBuilder in Java

Krantik Chavan
Updated on 26-Jun-2020 15:00:46

10K+ Views

In order to remove a substring from a Java StringBuilder Object, we use the delete() method. The delete() method removes characters in a range from the sequence. The delete() method has two parameters, start, and end. Characters are removed from start to end-1 index.Declaration − The java.lang.StringBuilder.delete() method is declared as follows −public StringBuilder delete(int start, int end)Let us see an example which removes a substring from a StringBuilder.Example Live Demopublic class Example {    public static void main(String[] args) {       StringBuilder sb = new StringBuilder("Welcome to Java Programming");       System.out.println("Original StringBuilder Object: " + sb.toString()); ... Read More

Remove characters in a range from a Java StringBuffer Object

Anvi Jain
Updated on 27-Jun-2020 13:20:44

2K+ Views

In order to remove characters in a specific range from a Java StringBuffer Object, we use the delete() method. The delete() method removes characters in a range from the sequence. The delete() method has two parameters , start and end. Characters are removed from start to end-1 index.Declaration − The java.lang.StringBuffer.delete() method is declared as follows −Let us see a program to delete characters in a specific range from a Java StringBuffer Object.Example Live Demopublic class Example {    public static void main(String[] args) {       StringBuffer sb = new StringBuffer("Hello World");       System.out.println("Original StringBuffer Object: " ... Read More

Remove a character from a Java StringBuffer object

Nitya Raut
Updated on 26-Jun-2020 15:04:51

479 Views

In order to remove a character from a Java StringBuffer object, we use the deleteCharAt() method. The deleteCharAt() removes the character at the specified index. The length of resultant sequence of characters of the StringBuffer object is reduced by one.Declaration − The java.lang.StringBuffer.deleteCharAt() method is declared as follows−public StringBuffer deleteCharAt(int index)Let us see a program to illustrate the use of the deleteCharAt()Example Live Demopublic class Example {    public static void main(String[] args) {       StringBuffer sb = new StringBuffer("Hello World");       sb.deleteCharAt(7);       System.out.println(sb);    } }OutputHello Wrld

Get the arc sine of an angle in Java

Nancy Den
Updated on 26-Jun-2020 15:05:58

247 Views

To get the arc sine of a given value in Java, we use the java.lang.Math.asin() method. The asin() method accepts a double value whose angle needs to be computed. The range of the angle returned lies in the range -pi/2 to pi/2. If the argument is NaN, then the result is NaN.When the argument is zero, then the output is a zero with the same sign as the argument.Declaration − The java.lang.Math.asin() method is declared as follows−public static double asin(double a)where a is the value whose arc sine is computed.Let us see a program to get the arc sine of ... Read More

Change a single character in a Java StringBuffer object in Java

Rishi Rathor
Updated on 26-Jun-2020 15:06:45

2K+ Views

In order to change a single character in a StringBuffer object in Java, we use the setCharAt() method. The setCharAt() method sets the character at the index specified as a parameter to another character whose value is passed parameter of the setCharAt() method. The method sets a new character sequence with the only change being the character passed as the parameter at the specified index.Declaration - The java.lang.StringBuffer.setCharAt() method is declared as follows−public void setCharAt(int index, char ch)If index is greater than the length of the StringBuffer object or is negative, an IndexOutOfBoundsException is generated.Let us see a program to ... Read More

Display the position of a Substring in Java

Nishtha Thakur
Updated on 26-Jun-2020 15:07:37

208 Views

To display the position of a Substring in Java, we use the lastindexOf() method in Java. The lastindexOf() method returns the rightmost occurrence of the substring within a particular StringBuffer object.If the substring occurs more than once in the StringBuffer object, the index of the first character of the rightmost occured substring is returned. If the substring is not found, the lastIndexOf() method returns -1.Declaration − The java.lang.StringBuffer.lastIndexOf() method is declared as follows−public int lastIndexOf(String s)where s is the substring whose index needs to be foundLet us see a program which displays the position of a Substring.Example Live Demopublic class Example ... Read More

Change the default capacity of the StringBuffer Object in Java

Nitya Raut
Updated on 26-Jun-2020 15:12:55

497 Views

In order to change the default capacity of the StringBuffer Object, we use the ensureCapacity() method. When the current capacity is less than the parameter passed, then a new internal array is allocated with greater capacity.The new capacity is the larger of the minimumCapacity argument and twice the old capacity, plus 2. If the minimum capacity is non positive, then it remains dormant and does not take any action.Declaration-The java.lang.StringBuffer.ensureCapacity() method is declared as follows−public void ensureCapacity(int minimumCapacity)Let us see the working of the ensureCapacity() methodExample Live Demopublic class Example {    public static void main(String[] args) {       ... Read More

Get the capacity of the StringBuffer Object in Java

Nancy Den
Updated on 26-Jun-2020 15:13:15

202 Views

The capacity() method is a method of the StringBuffer class . It returns the quantity of storage present for recently inserted characters, after which an allocation occurs.Declaration-The java.lang.StringBuffer.capacity() method is declared as follows−public int capacity()Let us see an example program showing how to get the capacity of the StringBuffer Object.Example Live Demoimport java.lang.*; public class Example {    public static void main(String[] args) {       StringBuffer b = new StringBuffer(“Hello”);       // returns the current capacity of the String buffer which is 16 + 5 = 21       System.out.println("Capacity for the StringBuffer Object = " + b.capacity());    } }OutputCapacity for the StringBuffer Object = 21

Change the length of the StringBuffer Object in Java

Rishi Rathor
Updated on 26-Jun-2020 15:16:28

722 Views

The setLength(int newLength) method is used to change the length of the StringBuffer Object. It sets the length of the character sequence. The character sequence is updated to a new one whose length is determined by the value passed as the parameter in the method. The newLength should be greater than or equal to zero.The java.lang.StringBuffer(int newLength) method is declared as follows −public void setLength(int newLength)Let us see a example program illustrating the use of setLength(int newLength) methodExample Live Demopublic class Example {    public static void main(String[] args) {       StringBuffer sb = new StringBuffer("Hello World");     ... Read More

Advertisements