集册 Java实例教程 如何将String数组转换为Java String对象。

如何将String数组转换为Java String对象。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

674
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
如何将String数组转换为Java String对象。


import java.util.Arrays;

 // 来自 时代Java公众号 - nowjava.com

public class Main {

        public static void main(String args[]){

                String[] strArray =

                        new String[]{"Java", "String", "Array", "To", "String", "Example"};

               

                /*

                 * 1. Using Arrays.toString method

                 */

               

                String str1 = Arrays.toString(strArray);               

                //replace starting "[" and ending "]" and ","

                str1 = str1.substring(1, str1.length()-1).replaceAll(",", "");

               

                System.out.println("String 1: " + str1);

                /*

                 * 2. Using Arrays.asList method followed by toString.

                 */

               

                String str2 = Arrays.asList(strArray).toString();

                //replace starting "[" and ending "]" and ","

                str2 = str2.substring(1, str2.length()-1).replaceAll(",", "");

                System.out.println("String 2: " + str2);/**n o w  j a v a  . c o m**/

               

                //3. Using StringBuffer.append method

                StringBuffer sbf = new StringBuffer();

               

                if(strArray.length > 0){

                        sbf.append(strArray[0]);

                        
展开阅读全文