简体   繁体   English

如何将字符数组转换回字符串?

[英]How to convert a char array back to a string?

I have a char array:我有一个字符数组:

char[] a = {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};

My current solution is to do我目前的解决方案是做

String b = new String(a);

But surely there is a better way of doing this?但肯定有更好的方法来做到这一点吗?

No, that solution is absolutely correct and very minimal.不,该解决方案是绝对正确的,而且非常少。

Note however, that this is a very unusual situation: Because String is handled specially in Java, even "foo" is actually a String .但是请注意,这是一种非常不寻常的情况:因为String在 Java 中是专门处理的,即使"foo"实际上也是一个String So the need for splitting a String into individual char s and join them back is not required in normal code.因此,在普通代码中不需要将 String 拆分为单独的char并将它们连接回来。

Compare this to C/C++ where "foo" you have a bundle of char s terminated by a zero byte on one side and string on the other side and many conversions between them due do legacy methods.将此与 C/C++ 中的"foo"进行比较,其中"foo"您有一堆char ,一侧以零字节string ,另一侧以string ,并且由于遗留方法,它们之间有许多转换。

String text = String.copyValueOf(data);

or要么

String text = String.valueOf(data);

is arguably better (encapsulates the new String call).可以说更好(封装了new String调用)。

This will convert char array back to string:这会将字符数组转换回字符串:

char[] charArray = {'a', 'b', 'c'};
String str = String.valueOf(charArray);
String str = "wwwwww3333dfevvv";
char[] c = str.toCharArray();

Now to convert character array into String , there are two ways.现在要将字符数组转换为 String ,有两种方法。

Arrays.toString(c);

Returns the string [w, w, w, w, w, w, 3, 3, 3, 3, d, f, e, v, v, v] .返回字符串[w, w, w, w, w, w, 3, 3, 3, 3, d, f, e, v, v, v]

And:和:

String.valueOf(c)

Returns the string wwwwww3333dfevvv .返回字符串wwwwww3333dfevvv

In Summary : pay attention to Arrays.toString(c) , because you'll get "[w, w, w, w, w, w, 3, 3, 3, 3, d, f, e, v, v, v]" instead of "wwwwww3333dfevvv" .总结:注意Arrays.toString(c) ,因为你会得到"[w, w, w, w, w, w, 3, 3, 3, 3, d, f, e, v, v, v]"而不是"wwwwww3333dfevvv"

A String in java is merely an object around an array of chars. java 中的 String 只是一个围绕字符数组的对象。 Hence a因此一个

char[]

is identical to an unboxed String with the same characters.与具有相同字符的未装箱字符串相同。 By creating a new String from your array of characters通过从您的字符数组创建一个新字符串

new String(char[])

you are essentially telling the compiler to autobox a String object around your array of characters.您实际上是在告诉编译器在您的字符数组周围自动装箱一个 String 对象。

You can use String.valueOf method.您可以使用String.valueOf方法。

For example,例如,

char[] a = {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};
String b = String.valueOf(a);
System.out.println("Char Array back to String is: " + b);

For more on char array to string you can refer links below有关字符数组到字符串的更多信息,您可以参考下面的链接

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html https://docs.oracle.com/javase/7/docs/api/java/lang/String.html

https://www.flowerbrackets.com/convert-char-array-to-string-java/ https://www.flowerbrackets.com/convert-char-array-to-string-java/

package naresh.java;

public class TestDoubleString {

    public static void main(String args[]){
        String str="abbcccddef";    
        char charArray[]=str.toCharArray();
        int len=charArray.length;

        for(int i=0;i<len;i++){
            //if i th one and i+1 th character are same then update the charArray
            try{
                if(charArray[i]==charArray[i+1]){
                    charArray[i]='0';                   
                }}
                catch(Exception e){
                    System.out.println("Exception");
                }
        }//finally printing final character string
        for(int k=0;k<charArray.length;k++){
            if(charArray[k]!='0'){
                System.out.println(charArray[k]);
            }       }
    }
}
 //Given Character Array 
  char[] a = {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};


    //Converting Character Array to String using String funtion     
    System.out.println(String.valueOf(a));
    //OUTPUT : hello world

Converting any given Array type to String using Java 8 Stream function使用 Java 8 Stream 函数将任何给定的数组类型转换为字符串

String stringValue = 
Arrays.stream(new char[][]{a}).map(String::valueOf).collect(Collectors.joining());

Just use String.value of like below;只需使用如下所示的 String.value 即可;

  private static void h() {

        String helloWorld = "helloWorld";
        System.out.println(helloWorld);

        char [] charArr = helloWorld.toCharArray();

        System.out.println(String.valueOf(charArr));
    }

Try to use java.util.Arrays .尝试使用java.util.Arrays This module has a variety of useful methods that could be used related to Arrays.该模块有多种有用的方法可以与数组相关。

Arrays.toString(your_array_here[]);

试试这个

Arrays.toString(array)
String output = new String(charArray);

其中 charArray 是字符数组,输出是转换为字符串的字符数组。

Try this:试试这个:

CharSequence[] charArray = {"a","b","c"};

for (int i = 0; i < charArray.length; i++){
    String str = charArray.toString().join("", charArray[i]);
    System.out.print(str);
}

You can also use StringBuilder class您还可以使用 StringBuilder 类

String b = new StringBuilder(a).toString();

Use of String or StringBuilder varies with your method requirements. String 或 StringBuilder 的使用因您的方法要求而异。

1 替代方法是做:

String b = a + "";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM