在 Java 中将对象转换为字符串
本篇文章介绍了如何在 Java 中将对象转换为字符串。
使用 Java 中的 valueOf()
方法将对象转换为字符串
String
类的 valueOf()
方法可以将对象转换为字符串。请参见下面的示例。
public class SimpleTesting{
public static void main(String[] args) {
Object obj = "DelftStack Portal";
System.out.println("Object value: "+obj);
String str = String.valueOf(obj);
System.out.println("String value: "+str);
}
}
输出:
Object value: DelftStack Portal
String value: DelftStack Portal
使用 Java 中的+
运算符将对象转换为字符串
在 Java 中,加号运算符+
将任何类型的值与字符串连接起来并返回结果字符串。我们也可以使用它将对象转换为字符串。请参见以下示例。
public class SimpleTesting{
public static void main(String[] args) {
Object obj = "DelftStack Portal";
System.out.println("Object value: "+obj);
String str = ""+obj;
System.out.println("String value: "+str);
}
}
输出:
Object value: DelftStack Portal
String value: DelftStack Portal
在 Java 中使用 toString()
方法将对象转换为字符串
Object
类的 toString()
方法将任何对象转换为字符串。请参见以下示例。
public class SimpleTesting{
public static void main(String[] args) {
Object obj = "DelftStack Portal";
System.out.println("Object value: "+obj);
String str = obj.toString();
System.out.println("String value: "+str);
}
}
输出:
Object value: DelftStack Portal
String value: DelftStack Portal
在 Java 中使用 toString()
方法将对象转换为字符串
对象可以是任何类型。例如,如果我们有一个整数对象并想要获取其字符串对象,请使用 toString()
方法。请参见下面的示例。
public class SimpleTesting{
public static void main(String[] args) {
Integer iVal = 123;
System.out.println("Integer Object value: "+iVal);
String str = iVal.toString();
System.out.println("String value: "+str);
}
}
输出:
Hello
This
is
DelfStack
在 Java 中使用 toString()
方法将对象转换为字符串
本示例说明了如何使用 toString()
方法将用户定义的对象转换为字符串。请参见下面的示例。
class Employee{
String fName;
String lName;
public Employee(String fName, String lName) {
this.fName = fName;
this.lName = lName;
}
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
@Override
public String toString() {
return "Employee [fName=" + fName + ", lName=" + lName + "]";
}
public String getString() {
return toString();
}
}
public class SimpleTesting{
public static void main(String[] args) {
Employee employee = new Employee("Rohan","Mosac");
System.out.println(employee.getString());
}
}
输出:
Employee [fName=Rohan, lName=Mosac]
使用 Java 中的 join()
方法将对象转换为字符串
在这里,我们使用 join()
方法将 ArrayList
对象转换为字符串。String
类的 join()
方法将它们连接到单个 String
对象中后返回一个字符串。请参见下面的示例。
import java.util.ArrayList;
import java.util.List;
public class SimpleTesting{
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Sun");
list.add("Moon");
list.add("Earth");
System.out.println("List object: "+list);
// list object to string
String str = String.join(",", list);
System.out.println("String: "+str);
}
}
输出:
List object: [Sun, Moon, Earth]
String: Sun,Moon,Earth
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中执行 SQL 查询
发布时间:2024/04/24 浏览次数:1195 分类:Python
-
本教程演示了在 Python 中对 Pandas DataFrame 执行 SQL 查询。
在 Pandas 中使用 stack() 和 unstack() 函数重塑 DataFrame
发布时间:2024/04/24 浏览次数:1289 分类:Python
-
本文讨论了 Pandas 中 stack() 和 unstack() 函数的使用。
在 Pandas 中读取 Excel 多张工作表
发布时间:2024/04/24 浏览次数:1450 分类:Python
-
本教程演示如何在 Pandas Python 中从 Excel 工作簿中读取多个 Excel 工作表。