Allocate a String that exhibits the sequence of characters present in a char array.
As String is immutable in Java, the consecutive modification of the character array does not impact the allocated String.
Using String Constructor
The String class provides inbuilt constructor which accepts a char array as an argument.
public static void main(String[] args) {
try {
char[] charArray = { 'W', 'E', 'L', 'C', 'O', 'M', 'E'};
String value = new String(charArray);
System.out.println("String Class Value is: " +value);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
String Class Value is: WELCOME
With StringBuilder
public static void main(String[] args) {
try {
final char[][] arrayCharArray = {{'w','e'},{'l','c'},{'o','m','e'}};
StringBuilder sb = new StringBuilder();
for (char[] childArray : arrayCharArray) {
sb.append(childArray);
}
System.out.println("After Forming as String: " +sb);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
After Forming as String: welcome
Using String.valueOf() Method
The String class provides valueOf() method which converts to string directly.
public static void main(String[] args) {
try {
char[] charArray = { 'W', 'E', 'L', 'C', 'O', 'M', 'E'};
String value = String.valueOf(charArray);
System.out.println("String Value is: " +value);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
String Value is: WELCOME
Using String.copyValueOf() Method
The String class provides another method copyValueOf() method.
We can use the Guava Common Base Joiner method, to convert from Character array to delimiter string value.
The import package is import com.google.common.base.Joiner;
public static void main(String[] args) {
try {
Character[] charArray = { 'W', 'E', 'L', 'C', 'O', 'M', 'E'};
String value = Joiner.on("-").join(charArray);
System.out.println("Joiner Value is: " +value);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
Joiner Value is: W-E-L-C-O-M-E
Convert a Byte Array to String in Java
As we know byte[] stores binary data and String stores text data.
So lets see how to convert a byte array to string with character encoding and without character encoding.
With Character Encoding:
It supports both UNICODE and ASCII format.
In this case, lets use StandardCharsets.UTF_8 to specify the type of character encoding type.
This tells how to encode the input characters into the sequence of bytes.
public static void main(String[] args) throws IOException
{
String value = "Byte Array";
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
// Now Create a string from a byte array with "UTF-8" encoding
String result = new String(bytes, StandardCharsets.UTF_8);
System.out.println(result);
}
Output:
Byte Array
Without Character Encoding:
Lets convert byte array to string without specifying the character encoding.
So here we declared an string variable and converted into bytes using getBytes() method.
Finally passed the bytes to String object instance to achieve the desired output.
public static void main(String[] args) throws IOException
{
String value = "Byte Array";
byte[] bytes = value.getBytes();
String result = new String(bytes);
System.out.println(result);
}
Output:
Byte Array
Iterate through the characters of a string in Java
Lets use for loop function to iterate the number of characters of a String.
Apply charAt() method to retrieve each character and check it.
public static void main(String[] args) throws IOException
{
String value = "char";
for (int i = 0; i < value.length(); i++){
char result = value.charAt(i);
System.out.println("printing : "+result);
}
}
Output:
printing : c
printing : h
printing : a
printing : r
Lets look at efficient way of using for loop function with below code snippet.
public static void main(String[] args) throws IOException
{
String value = "char";
for(char result : value.toCharArray()) {
System.out.println("printing : "+result);
}
}
Output:
printing : c
printing : h
printing : a
printing : r
Tip:
Unlike C, There’s no such entity as NULL in Java. Null is not a valid value for a char variable.
A char can have a integer value of zero, which has no particular significance.
To conclude this java tutorial, we covered various examples on converting char Array to a String in Java.
Looking to convert List to String in Java? Let’s have a look at how to convert a Java Collections List of elements to a String in Java. In this post, we have talked about these best 12 methods of convert list string in java, you can learn and also implement it easily.
To Convert a list to string in java, You can do by,
1. Using toString()
2. List Of Object To String
3. Join Method(StringUtils)
4. Stream Collectors
5. Comma Separator(delimiter)
6. Convert Character List To String
7. With Double Quotes
8. Sort Method
9. toJSON Method
10. The method in Java 8
11. ReplaceAll Method
12. LinkList to String
Introduction to Convert List to String in Java
Basically List in Java is an ordered collection or a default sequence.
List accepts duplicate elements unlike Map doesn’t accept duplicate values and also holds the object in key-value pairs.
We can print the contents of a List element in a readable form while debugging the code, which is helpful.
List interface and String class were part of Java object-oriented programming API.
We can add any type of Java object to a List. If the List does not type, then using Java Generics we can apply objects of various types in the same List.
Typically we will enclose the generic type in square brackets.
Using toString()
From the below Java program, let’s see how to convert Java list to string array using the toString method.
We are passing an integer argument(number) to create a java string array using the Java ArraysasList() method.
In other words, we are passing a list of integers as an array to list.
Lets convert Listto String using replaceAll() and String.join() method.
You can also notice inline lambda expressions used in the replaceAll method.
public static void main(String[] args) {
try {
List<String> countries = Arrays.asList("usa", "uk", "india");
countries.replaceAll(r -> r.toUpperCase());
String result = String.join(" ", countries);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
USA UK INDIA
LinkedList to String
Let’s convert LinkedList to String using String.join() method.
public static void main(String[] args) {
try {
LinkedList<String> list = new LinkedList();
list.add("USA");
list.add("UK");
list.add("INDIA");
String result = String.join(" ", list);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
USA UK INDIA
In case of any issues during conversion, we can notice the error message in the console log.
To conclude, in this tutorial we gone through different ways to convert a Java List to String data type.
List to String methods conversion is also common in other programming languages like JavaScript, Python, Jquery.
In a nutshell, JavaScript uses an str function for concatenate, likewise, the python program uses join method for conversion and concatenate a string.