Different methods to append a single character to a string or char array in Java


Have you ever faced the situation of extending a string or char array? If you haven't yet, you might probably encounter this situation in the future. It is a common practice to append a single character to a string or char array in Java.

The significant difference between a string array and a char array is that a string array is a sequence of characters, and a char array is a sequence of collection of data type char. String array runs as a single entity, whereas char array runs as a separate entity.

In this blog, we will look into what is the append method and what are the four methods you can use to extend the sequence data. Let's get started.

What is the append method?

The append method in Java is a method that is used to insert new data into the existing file. While working with strings, you might probably face a situation where you need to append a character to a string array or char array. In that case, you have four ways to move ahead. You can choose the best way by analyzing the output you desire for. The two methods are,

  • StringBuffer method

  • System.arraycopy() method.

  • StringBuilder method

  • String Concat (+)

Method 1: StringBuffer method

Generally, a string is immutable, i.e., the sequence data of the string cannot be changed. However, using the StringBuffer method, you can extend and add characters to the string. In short, unlike string, StringBuffer is mutable character data. You can extend or add objects once it is created. Moreover, string tends to consume more memory and is relatively slower than StringBuffer. Whereas StringBuffer takes up only less memory. Additionally, StringBuffer is recommended while performing concatenation as the process is faster.

How to append a single character in the StringBuffer class?

To append a string to the existing StringBuffer class, we are going to use the append method. By following this method, you can add a new character to your string array or char array. You have to insert the character you want to add to the object in StringBuffer sb = new StringBuffer();

public class Test { public static void main(String args[]) { StringBuffer sb = new StringBuffer("Hi what are you doing"); sb.append("u"); System.out.println(sb); } }

Output

Hi what are you doing

Method 2: System.arraycopy() method

The next way to append a character to a string is by leveraging the System.arraycopy() method. The key concept of this method is to copy one array of characters to another. There are four arguments involved. Arguments are the values that are passed on while functioning. The first argument is the source array, and the second argument isconsidered to be the source index.

In short, the position from where you copy the array is the second argument. The thirdargument is the destination array where we copy. The fourth argument is considered the destination index.

How to append using System.arraycopy() method?

Here we provide you an instance for the system.arraycopy() method.

char[] src = {’H’, ‘e’, ‘l’, ‘l’, ‘o’};
char[] dest = new char[src.length + 1];
System.arraycopy(src, 0, dest, 0, src.length);
dest[src.length] = ‘!’; // now the contents of dest are “Hello!”

Method 3: StringBuilder method

While you Concatenate many strings, using the StringBuilder method would be wise. Like StringBuffer, StringBuilder is also mutable,i.e., it can change the object. However, StringBuilder is preferred over StringBuffer as it is faster when compared to the StringBuffer. The concept behind StringBuilder is to convert a string to StringBuilder, as the string is immutable. Once done, you can convert it back to a string.

How to use the StringBuilder method?

Here’s a code to append characters to the end of the string by applying the StringBuilder method.

public class JavaLanguage { public static void main(String args[]) { StringBuilder str = new StringBuilder("JavaLanguage"); str.append('A'); System.out.println(str); } }

Output

JavaLanguageA

Method 4: Using the String concat () method

The final way is to use the String concatenation operation () method to add a character to the end of the string. The concept of this method is that a new mutable string will be created if the existing string cannot be changed. Then append the character and convert it back to the string.

How to use String Concat () method?

Here’s Java program illustrating how to append the string at the end of the string using concat() method.

public class Test { public static void main(String args[]) { String s = "All Indians"; s = s.concat("are my brothers and sisters"); System.out.println(s); } }

Output

All Indians are my brothers and sisters 

Wrapping up

With that, we have reached the end of today's concept. Here is a quick recap of what we discussed today. We discussed what the append method is. Then we discussed different ways to append a single character to a string or character array. We have also presented you with examples and codes to refer to. If you have any other queries reach out to us or comment below.

Updated on: 25-Aug-2022

639 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements