Java StringBuilder Archives - Huong Dan Java https://huongdanjava.com/tag/java-stringbuilder-en Java development tutorials Mon, 01 Nov 2021 14:15:34 +0000 en-US hourly 1 https://wordpress.org/?v=6.9.4 https://huongdanjava.com/wp-content/uploads/2018/01/favicon.png Java StringBuilder Archives - Huong Dan Java https://huongdanjava.com/tag/java-stringbuilder-en 32 32 Methods of the StringBuilder object in Java https://huongdanjava.com/methods-of-the-stringbuilder-object-in-java.html https://huongdanjava.com/methods-of-the-stringbuilder-object-in-java.html#respond Mon, 10 Dec 2018 23:08:55 +0000 https://huongdanjava.com/?p=12512 In addition to the charAt(), indexOf(), substring(), length() methods that work in the String object, StringBuilder has other main methods that we’ll cover in the following: The append() method adds a value at the end of the current string of the StringBuilder object. We can… Read More

The post Methods of the StringBuilder object in Java appeared first on Huong Dan Java.

]]>
In addition to the charAt(), indexOf(), substring(), length() methods that work in the String object, StringBuilder has other main methods that we’ll cover in the following:

The append() method adds a value at the end of the current string of the StringBuilder object. We can add a variety of data types using overloading methods of this method.
For example:

Learn about StringBuilder object in Java

The insert() method inserts data at any location in the StringBuilder object.
For example:

Learn about StringBuilder object in Java

The delete() method with two start and end arguments is used to delete a string with the start and end indexes.
For example:

Learn about StringBuilder object in Java

Note that this method will not delete the character at the end index!

The deleteCharAt() method deletes a character at any location in the string of the StringBuilder object.
For example:

Learn about StringBuilder object in Java

The reverse() method reverses the characters in the string of the StringBuilder object.
For example:

Learn about StringBuilder object in Java

The replace() method replaces any string from the start index to the end index in the string of the StringBuilder object.
For example:

Learn about StringBuilder object in Java

As the delete() method, the character at the end will not be counted in the character to replace!

The subsequence() method takes a string from the start index to the end index in the string of the StringBuilder object.
Note that using the subsequence() method will not change our StringBuilder object, and the new string will not have the character at the end.

For example:

Learn about StringBuilder object in Java

The post Methods of the StringBuilder object in Java appeared first on Huong Dan Java.

]]>
https://huongdanjava.com/methods-of-the-stringbuilder-object-in-java.html/feed 0
Learn about StringBuilder object in Java https://huongdanjava.com/learn-about-stringbuilder-object-in-java.html https://huongdanjava.com/learn-about-stringbuilder-object-in-java.html#respond Mon, 10 Dec 2018 22:51:38 +0000 https://huongdanjava.com/?p=12484 We often use the StringBuilder object to build String object whose content is aggregated from a variety of conditions. In this tutorial, I will summarize the knowledge needed to work with the StringBuilder object! Initialize the StringBuilder object StringBuilder gives us 4 constructors to initialize… Read More

The post Learn about StringBuilder object in Java appeared first on Huong Dan Java.

]]>
We often use the StringBuilder object to build String object whose content is aggregated from a variety of conditions. In this tutorial, I will summarize the knowledge needed to work with the StringBuilder object!

Initialize the StringBuilder object

StringBuilder gives us 4 constructors to initialize a StringBuilder object. That is:

A constructor with no parameters.
For example:

StringBuilder sb = new StringBuilder();

A constructor with a parameter defines the initial capacity of the StringBuilder object.
For example:

StringBuilder sb = new StringBuilder(12);

A constructor with a parameter as an implementation of CharSequence interface.
A constructor with a parameter as a String object.
For example:

StringBuilder sb = new StringBuilder("Khanh");



The concept of capacity and the StringBuilder object

In terms of initializing the StringBuilder object, I mentioned the concept of capacity. What is the capacity, I would like to say more about it for your understanding?

The capacity here is the ability to contain a String object with a certain length of our StringBuilder object.

By default, if you initialize a StringBuilder object with a constructor without a parameter, the capacity of this object is 16. With the constructor initialized with a capacity, the object created will have the initial capacity equal to the capacity that we pass on.

In the other two cases, the capacity is equal to the length of the CharSequence object or String, plus with 16.

Although there is a capacity you are assured, when you add String with a length greater than capacity, StringBuilder will automatically increase capacity for us so there will not be any problems here.

The post Learn about StringBuilder object in Java appeared first on Huong Dan Java.

]]>
https://huongdanjava.com/learn-about-stringbuilder-object-in-java.html/feed 0
The ways to convert from List of String object to String object in Java https://huongdanjava.com/the-ways-to-convert-from-list-of-string-object-to-string-object-in-java.html https://huongdanjava.com/the-ways-to-convert-from-list-of-string-object-to-string-object-in-java.html#respond Thu, 09 Feb 2017 15:46:23 +0000 https://huongdanjava.com/?p=2100 In Java, we have a lot of ways to convert List of String object to String object with a delimiter between the elements in List object. In this tutorial, I will share with you all some ways as below: The first one, we will use StringBuilder… Read More

The post The ways to convert from List of String object to String object in Java appeared first on Huong Dan Java.

]]>
In Java, we have a lot of ways to convert List of String object to String object with a delimiter between the elements in List object. In this tutorial, I will share with you all some ways as below:

The first one, we will use StringBuilder object in Java.

We will read all elements in the List, one by one and use StringBuilder to add the delimiter among all elements. Note that, with the first element, we will not add the delimiter!

In detail, the method can be written as below:

public static String join(List<String> list, char delimiter) {
    StringBuilder sb = new StringBuilder();
 
    for (int i = 0; i < list.size(); i++) {
        String s = list.get(i);
        if (i == 0) {
            sb.append(s);
            continue;
        }
        sb.append(delimiter + s);
    }
 
    return sb.toString();
}

Example:

The ways to convert from List of String object to String object in Java

The second one, we will use collect() method of Stream object which was introduced since Java 8.

public static String join(List<String> list, char delimiter) {
	return list.stream().collect(Collectors.joining(String.valueOf(delimiter)));
}

Example:

The ways to convert from List of String object to String object in Java

The third one, we will use join() static method of String object.

Since Java 8, Java introduced a new method named join() in String object, help us can convert from List object to String object easily.

public static String join(List<String> list, char delimiter) {
	return String.join(String.valueOf(delimiter), list);
}

Example:

The ways to convert from List of String object to String object in Java

Finally, we can use an existing library Apache Commons Lang of Apache Foundation.

This library provides a static method, named join() in StringUtils class to help us can convert a List object to String object simply and easily. Because of its static method, then we only need call:

StringUtils.join(List list, char delimiter);

Example:

The ways to convert from List of String object to String object in Java

The post The ways to convert from List of String object to String object in Java appeared first on Huong Dan Java.

]]>
https://huongdanjava.com/the-ways-to-convert-from-list-of-string-object-to-string-object-in-java.html/feed 0