java字符串转字符串数组
Today we will look into how to convert Java String array to String. Sometimes we have to convert String array to String for specific requirements. For example; we want to log the array contents or we need to convert values of the String array to String and invoke other methods.
今天,我们将研究如何将Java String数组转换为String。 有时,为了满足特定要求,我们必须将String数组转换为String。 例如; 我们要记录数组内容,或者需要将String数组的值转换为String并调用其他方法。
Java字符串数组到字符串 (Java String Array to String)
Most of the time we invoke toString() method of an Object to get the String representation. Let’s see what happens when we invoke toString() method on String array in java.
大多数时候,我们调用Object的toString()方法来获取String表示形式。 让我们看看在Java中对String数组调用toString()方法时会发生什么。
package com.journaldev.util;
public class JavaStringArrayToString {
public static void main(String[] args) {
String[] strArr = new String[] {"1","2","3"};
String str = strArr.toString();
System.out.println("Java String array to String = "+str);
}
}
Below image shows the output produced by the above program.
下图显示了以上程序产生的输出。
The reason for the above output is because toString() call on the array is going to Object superclass where it’s implemented as below.
上面输出的原因是因为对数组的toString()调用将转到Object超类,该超类的实现如下。
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
Java字符串数组到字符串示例 (Java String Array to String Example)
So how to convert String array to String in java. We can use Arrays.toString method that invoke the toString() method on individual elements and use StringBuilder to create String.
那么如何在Java中将String数组转换为String。 我们可以使用Arrays.toString方法在单个元素上调用toString()方法,并使用StringBuilder创建String。
public static String toString(Object[] a) {
if (a == null)
return "null";
int iMax = a.length - 1;
if (iMax == -1)
return "[]";
StringBuilder b = new StringBuilder();
b.append('[');
for (int i = 0; ; i++) {
b.append(String.valueOf(a[i]));
if (i == iMax)
return b.append(']').toString();
b.append(", ");
}
}
We can also create our own method to convert String array to String if we have some specific format requirements.
如果我们有一些特定的格式要求,我们还可以创建自己的方法将String数组转换为String。
Below is a simple program showing these methods in action and output produced.
下面是一个简单的程序,显示了这些方法的作用和所产生的输出。
package com.journaldev.util;
import java.util.Arrays;
public class JavaStringArrayToString {
public static void main(String[] args) {
String[] strArr = new String[] { "1", "2", "3" };
String str = Arrays.toString(strArr);
System.out.println("Java String array to String = " + str);
str = convertStringArrayToString(strArr, ",");
System.out.println("Convert Java String array to String = " + str);
}
private static String convertStringArrayToString(String[] strArr, String delimiter) {
StringBuilder sb = new StringBuilder();
for (String str : strArr)
sb.append(str).append(delimiter);
return sb.substring(0, sb.length() - 1);
}
}
So if we use array toString() method, it returns useless data.
因此,如果我们使用数组toString()方法,它将返回无用的数据。
Java Arrays class provide toString(Object[] objArr) that iterates over the elements of the array and use their toString() implementation to return the String representation of the array. That’s why when we use this function, we can see that it’s printing the array contents and it can be used for logging purposes.
Java Arrays类提供了toString(Object[] objArr) ,该方法可以迭代数组的元素,并使用其toString()实现返回数组的String表示形式。 这就是为什么当我们使用此函数时,我们可以看到它正在打印数组内容,并且可以用于记录目的。
If you want to combine all the String elements in the String array with some specific delimiter, then you can use convertStringArrayToString(String[] strArr, String delimiter) method that returns the String after combining them.
如果要将String数组中的所有String元素与某些特定的定界符组合在一起,则可以使用convertStringArrayToString(String[] strArr, String delimiter)方法,该方法在组合它们后返回String。
Java数组到字符串的示例 (Java Array to String Example)
Now let’s extend our String array to String example to use with any other custom classes, here is the implementation.
现在让我们将String数组扩展为String示例,以与任何其他自定义类一起使用,这是实现。
package com.journaldev.util;
import java.util.Arrays;
public class JavaArrayToString {
public static void main(String[] args) {
A[] arr = { new A("1"), new A("2"), new A("3") };
// default toString() method
System.out.println(arr.toString());
// using Arrays.toString() for printing object array contents
System.out.println(Arrays.toString(arr));
// converting Object Array to String
System.out.println(convertObjectArrayToString(arr, ","));
}
private static String convertObjectArrayToString(Object[] arr, String delimiter) {
StringBuilder sb = new StringBuilder();
for (Object obj : arr)
sb.append(obj.toString()).append(delimiter);
return sb.substring(0, sb.length() - 1);
}
}
class A {
private String name;
public A(String name) {
this.name = name;
}
@Override
public String toString() {
System.out.println("A toString() method called!!");
return this.name;
}
}
Output produced by the above java array to String example program is;
上面的java数组产生的输出到String示例程序是:
[Lcom.journaldev.util.A;@7852e922
A toString() method called!!
A toString() method called!!
A toString() method called!!
[1, 2, 3]
A toString() method called!!
A toString() method called!!
A toString() method called!!
1,2,3
So we looked at how to convert Java String array to String and then extended it to use with custom objects. That’s all for converting java array to String.
因此,我们研究了如何将Java String数组转换为String,然后将其扩展为与自定义对象一起使用。 这就是将java数组转换为String的全部。
GitHub Repository.
GitHub Repository中检出更多核心Java示例。
Reference: Java Arrays toString API Doc
参考: Java Arrays toString API文档