如何在Java中将字符转换为字符串?
如果我们有一个像 'G' 这样的 char 值,并且我们想将它转换成一个等价的字符串,比如“G”,那么我们可以通过使用Java中列出的以下四种方法中的任何一种来做到这一点:
插图:
Input : 'G'
Output : "G"
方法:
有多种方法可以使用包装类和Java类中提供的方法将所需的字符转换为字符串。
- 使用字符串连接
- 使用字符类的 toString() 方法
- 使用字符包装类
- 使用 String 类的 valueOf() 方法
让我们在下面用干净的Java程序详细讨论这些方法,如下所示:
方法一:使用字符串串联
我们可以通过将给定字符与空字符串连接来将 char 转换为Java中的字符串对象。
例子
Java
// Java Program to Convert Char to String
// Using Concatenation in Strings
// Importing the required packages
import java.io.*;
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring a char variable
char c = 'G';
// Concatenating the char variable
// with an empty string
String s = "" + c;
// Print and display the above string
System.out.println(
"Char to String using Concatenation :"
+ " " + s);
}
}
Java
// Java Program to Convert Char to String
// Using toString() method of Character class
// Importing the required packages
import java.io.*;
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring a char variable
char c = 'G';
// Converting the char to String using toString
// method of Character class and storing it in a
// string
String s = Character.toString(c);
// Print and display the above string
System.out.println(
"Char to String using Character.toString method :"
+ " " + s);
}
}
Java
// Java Program to Convert Char to String
// Using toString() method of Character class
// Importing the required packages
import java.io.*;
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring a char variable
char c = 'G';
// Converting the char to String using toString
// method of Character class and storing it in a
// string
String s = Character.toString(c);
// Print and display the above string
System.out.println(
"Char to String using Character.toString method :"
+ " " + s);
}
}
Java
// Java Program to Convert Char to String
// Using String.valueOf() method of String class
// Importing the required packages
import java.io.*;
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring a char variable
char c = 'G';
// Converting char to String by
// using String.valueOf() method
String s = String.valueOf(new char[] { c });
// Print and display the above-stored string
System.out.println(
"Char to String using String.valueOf(new char[]) method :"
+ " " + s);
}
}
Java
// Java Program to Convert Char to String
// Using valueOf() method of String class
// Importing the required packages
import java.io.*;
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring a char variable
char c = 'G';
// Converting char to String
// using String.valueOf() method
String s = String.valueOf(c);
// Print and display the String s
System.out.println(
"Char to String using String.valueOf() method :"
+ " " + s);
}
}
Char to String using Concatenation : G
方法二:使用字符类的 toString() 方法
我们可以使用字符 .toString() 方法将 char 转换为Java中的字符串对象。
例子
Java
// Java Program to Convert Char to String
// Using toString() method of Character class
// Importing the required packages
import java.io.*;
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring a char variable
char c = 'G';
// Converting the char to String using toString
// method of Character class and storing it in a
// string
String s = Character.toString(c);
// Print and display the above string
System.out.println(
"Char to String using Character.toString method :"
+ " " + s);
}
}
Char to String using Character.toString method : G
方法3:使用字符包装类
我们可以使用Java .lang 将 char 转换为Java中的字符串对象。字符类,它是 char 原始类型的包装器。
Note: This method may arise a warning due to the new keyword as Character(char) in Character has been deprecated and marked for removal.
例子
Java
// Java Program to Convert Char to String
// Using toString() method of Character class
// Importing the required packages
import java.io.*;
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring a char variable
char c = 'G';
// Converting the char to String using toString
// method of Character class and storing it in a
// string
String s = Character.toString(c);
// Print and display the above string
System.out.println(
"Char to String using Character.toString method :"
+ " " + s);
}
}
方法 4-A:使用 String 类的 String.valueOf() 方法
我们可以使用 String.valueOf(char[]) 方法将 char 转换为Java中的字符串对象。
例子
Java
// Java Program to Convert Char to String
// Using String.valueOf() method of String class
// Importing the required packages
import java.io.*;
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring a char variable
char c = 'G';
// Converting char to String by
// using String.valueOf() method
String s = String.valueOf(new char[] { c });
// Print and display the above-stored string
System.out.println(
"Char to String using String.valueOf(new char[]) method :"
+ " " + s);
}
}
Char to String using String.valueOf(new char[]) method : G
方法 4-B:使用 String 类的 valueOf() 方法
我们可以使用 String.valueOf() 方法将 char 转换为Java中的字符串对象。
例子
Java
// Java Program to Convert Char to String
// Using valueOf() method of String class
// Importing the required packages
import java.io.*;
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring a char variable
char c = 'G';
// Converting char to String
// using String.valueOf() method
String s = String.valueOf(c);
// Print and display the String s
System.out.println(
"Char to String using String.valueOf() method :"
+ " " + s);
}
}
Char to String using String.valueOf() method : G