Jak przekonwertować tablicę znaków z powrotem na łańcuch znaków?

Mam tablicę znaków:

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

Moim obecnym rozwiązaniem jest zrobić

String b = new String(a);
Ale na pewno jest na to lepszy sposób?
Author: Sam, 2011-10-05

11 answers

Nie, to rozwiązanie jest absolutnie poprawne i bardzo minimalne.

Zauważ jednak, że jest to bardzo nietypowa sytuacja: ponieważ {[0] } jest obsługiwany specjalnie w Javie, nawet "foo" jest w rzeczywistości String. Tak więc w normalnym kodzie nie jest wymagana potrzeba dzielenia łańcuchów na poszczególne chars i łączenia ich z powrotem.

Porównaj to z C / C++ gdzie "foo" masz pakiet char s zakończony bajtem zerowym po jednej stronie i string po drugiej stronie i wiele konwersji między nimi z powodu starsze metody.

 179
Author: A.H.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-10-04 23:30:29

String text = String.copyValueOf(data);

Lub

String text = String.valueOf(data);

Jest prawdopodobnie lepsza (enkapsuluje wywołanie new String).

 134
Author: David Titarenco,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-09 20:31:48

To skonwertuje tablicę znaków z powrotem na łańcuch znaków:

char[] charArray = {'a', 'b', 'c'};
String str = String.valueOf(charArray);
 35
Author: Billz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-10 19:56:37
String str = "wwwwww3333dfevvv";
char[] c = str.toCharArray();

Teraz, Aby przekonwertować tablicę znaków na ciąg znaków, są dwa sposoby.

Arrays.toString(c);

Zwraca łańcuch [w, w, w, w, w, w, 3, 3, 3, 3, d, f, e, v, v, v].

I:

String.valueOf(c)

Zwraca łańcuch wwwwww3333dfevvv.

 5
Author: AalekhG,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-27 11:10:01

Łańcuch znaków w Javie jest jedynie obiektem wokół tablicy znaków. Stąd a

char[]

Jest identyczny z nieboxowanym ciągiem o tych samych znakach. Tworząc nowy ciąg znaków z tablicy znaków

new String(char[])

W zasadzie mówisz kompilatorowi autoboxowi obiekt String wokół tablicy znaków.

 4
Author: Nathan Meyer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-15 13:52:36
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]);
            }       }
    }
}
 2
Author: Ramnaresh Mantri,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-04 20:28:34

Możesz użyć metody String.valueOf.

Na przykład,

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);

Więcej informacji na temat tablicy znaków do łańcucha można znaleźć poniżej

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

Http://www.flowerbrackets.com/char-array-to-string-java-program/

 1
Author: Shiva,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-19 10:44:57

Spróbuj tego

Arrays.toString(array)
 0
Author: Jessica Pereira,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-24 23:33:34

Spróbuj tego:

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

for (int i = 0; i < charArray.length; i++){
    String str = charArray.toString().join("", charArray[i]);
    System.out.print(str);
}
 -2
Author: Curtis H,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-18 23:23:11

Możesz również użyć klasy StringBuilder

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

Użycie String lub StringBuilder różni się w zależności od wymagań metody.

 -4
Author: Jagmeet Singh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-16 13:23:31

1 alternatywny sposób to zrobić:

String b = a + "";
 -11
Author: Prasanna A,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-22 11:05:23