在 Java 中将对象转换为字符串 您所在的位置:网站首页 如何将object转换为string 在 Java 中将对象转换为字符串

在 Java 中将对象转换为字符串

2023-10-26 16:45| 来源: 网络整理| 查看: 265

使用 Java 中的 valueOf() 方法将对象转换为字符串 使用 Java 中的+ 运算符将对象转换为字符串 在 Java 中使用 toString() 方法将对象转换为字符串 在 Java 中使用 toString() 方法将对象转换为字符串 在 Java 中使用 toString() 方法将对象转换为字符串 使用 Java 中的 join() 方法将对象转换为字符串 在 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); } } Ezoic

输出:

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

输出:

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

输出:

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

输出:

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


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有