StringBuffer Append Method in Java

Rishi Rathor
Updated on 26-Jun-2020 15:34:24

184 Views

The StringBuffer append() method appends the String representation of the particular argument to the sequence. It is a method of the java.lang.StringBuffer class. This method returns a reference to the object.The basic syntax for append() method is as follows −public StringBuffer append(data_type variable_name)A program to illustrate the use of append() method is given as follows −Example Live Demoimport java.lang.*; public class Example {    public static void main(String[] args) {       StringBuffer s1 = new StringBuffer("The sun rises in the east ");       System.out.println("Statement : " + s1);       s1.append(true);       System.out.println("Outcome : ... Read More

Convert String to Integer Using Integer.parseInt in Java

Samual Sam
Updated on 26-Jun-2020 15:31:54

268 Views

The Integer.parseInt() method in Java converts a string to an integer.Let’s say the following is our string −String str = "456";Converting it into integer −Integer.parseInt(str));The following is the complete example −Example Live Demopublic class Demo {     public static void main(String[] args) {        String str = "456";        System.out.println(Integer.parseInt(str));     } }Output456

Convert String of Time to Time Object in Java

karthikeya Boyini
Updated on 26-Jun-2020 15:31:06

10K+ Views

Here is our string.String strTime = "20:15:40";Now, use the DateFormat to set the format for date.DateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");Parse the string of time to time object.Date d = dateFormat.parse(strTime);The following is the complete example.Example Live Demoimport java.text.DateFormat; import java.util.Date; import java.text.SimpleDateFormat; public class Demo {     public static void main(String[] args) throws Exception {        String strTime = "20:15:40";        DateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");        Date d = dateFormat.parse(strTime);        System.out.println("Resultant Date and Time = " + d);     } }OutputResultant Date and Time = Thu Jan 01 20:15:40 UTC 1970

Call append Method to Construct a StringBuffer Object in Java

Nancy Den
Updated on 26-Jun-2020 15:29:55

244 Views

The StringBuffer append() method appends the String representation of the particular argument to the sequence. It is a method of the java.lang.StringBuffer class. This method returns a reference to the object.The method changes the value of the object called in the method. Each primitive data type will have a separate method −public StringBuffer append(double d) public StringBuffer append(float f) public StringBuffer append(int i) public StringBuffer append(String str) public StringBuffer append(long l) public StringBuffer append(char[] str) public StringBuffer append(char[] str, int offset, int len) public StringBuffer append(Object obj) public StringBuffer append(boolean b) public StringBuffer append(char c) public StringBuffer append(StringBuffer sb)ExampleA program ... Read More

Search Index of a Character in a String in Java

Samual Sam
Updated on 26-Jun-2020 15:28:38

1K+ Views

Use the indexOf() method to search the index of a character in a string.Let’s say the following is our string.String myStr = "amit";Now, let us find the index of character ‘t’ in the above string.strIndex = myStr.indexOf('t');The following is the final example.Example Live Demopublic class Demo {     public static void main(String[] args) {        String myStr = "amit";        int strIndex = 0;        System.out.println("String: "+myStr);        // finding index of character t        strIndex = myStr.indexOf('t');        System.out.println("Character m found at index: "+strIndex);     } }OutputString: amit Character m found at index: 3

Get Length of a StringBuffer Object in Java

Nishtha Thakur
Updated on 26-Jun-2020 15:17:13

896 Views

To find the length of the StringBuffer object, the length() function is used. It is a method of the StringBuffer Class. The method returns the count of characters in the sequence.Declaration- The java.lang.StringBuffer.length() method is declared as follows −public int length()Let us see an example program illustrating the use of length() method −Example Live Demopublic class Example {    public static void main(String[] args) {       StringBuffer sb = new StringBuffer("Hello World");       int len = sb.length(); // computes the length of the StringBuffer Object       System.out.println(len);    } }Output11

Change Length of StringBuffer Object in Java

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

703 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

Get the Capacity of the StringBuffer Object in Java

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

187 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 Default Capacity of StringBuffer Object in Java

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

474 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

Find Last Occurrence of a Character in a String in Java

karthikeya Boyini
Updated on 26-Jun-2020 15:12:51

2K+ Views

Use the lastIndexOf() method to find the last occurrence of a character in a string in Java.Let’s say the following is our string.String myStr = "Amit Diwan";In the above string, we will find the last occurrence of character ‘i’myStr.lastIndexOf('i');The following is the complete example.Example Live Demopublic class Demo {  public static void main(String[] args) {     String myStr = "Amit Diwan";     int strLastIndex = 0;     System.out.println("String: "+myStr);     strLastIndex = myStr.lastIndexOf('i');     System.out.println("The last index of character a in the string: "+strLastIndex);  } }OutputString: Amit Diwan The last index of character a in the string: 6Read More

Advertisements