1. A conversão mútua entre StringBuffer e String
1. Converta StringBuffer em string
O membro da função ToString da classe StringBuffer pode convertê -la em um tipo de string.
StringBuffer buffer = newsTringBuffer ("abcd"); string str = buffer.toString (); Converta uma classe StringBuffer em uma classe String através da construção na classe String: String(StringBuffer buffer)
StringBuffer buffer = newsTringBuffer ("ABCD"); String str = Newstring (buffer);2. Converta String em StringBuffer
Método 1: Use construtores
String str = "Hello World."; StringBuffer buffer = new StringBuffer (str);
Método 2: Ligue para a função de apêndice
String str = "Hello World."; StringBuffer buffer = new StringBuffer (); buffer.append (str);
2. A conversão mútua entre a matriz de corda e o caractere
1. Converta a string em uma matriz de caracteres
A função ToCharArray do membro da classe String pode convertê -la em uma matriz de caracteres.
String str = "Hello World."; // Crie um objeto String char [] ch = str.toCharArray (); //, então chame a função toCharArray do objeto String para convertê -lo em uma palavra
2. Converta a matriz de caracteres em string
Método 1: use o construtor da classe String para concluir a conversão diretamente ao construir string.
char [] data = {'a', 'b', 'c'}; string str = new String (dados);Método 2: Chame a conversão da função ValueOf da classe String.
String.valueof (char [] ch);
3. Converta StringBuffer e matriz de caracteres um para o outro
1. Converta StringBuffer em matriz de caracteres
A conversão diretamente do StringBuffer para a matriz de caracteres não é suportada em Java. Em vez disso, converta o StringBuffer em string primeiro.
Em seguida, a função toCharArray é chamada por String para convertê -la em uma matriz de caracteres.
StringBuffer stringBuffer = new StringBuffer ("Hello World."); String str = StringBuffer.toString (); // primeiro converte o objeto StringBuffer em string objeto char [] ch = str.toCharArray (); //, então chama a função TOCharArray do objeto String para convertê -lo em uma matriz do caractere2. Converta a matriz de caracteres em StringBuffer
Semelhante à conversão de um StringBuffer em uma matriz de caracteres, você precisa converter a matriz de caracteres em uma string primeiro e depois convertê -la de uma string em um StringBuffer.
char [] dados = {'h', 'e', 'l', 'l', 'o', 'd'}; string str = new String (); // ou chama diretamente o construtor: string str = new string (dados); str = string.valueof (dados); buffer.append (str); // chamando a função Anex para converter string em stringbufferResumir
O exposto acima é a conversão entre a matriz de caracteres, a classe String e o StringBuffer em Java apresentado a você. Espero que seja útil para você. Se você tiver alguma dúvida, deixe -me uma mensagem e o editor responderá a você a tempo. Muito obrigado pelo seu apoio ao site wulin.com!