Java 如何将任何对象转换为字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31847080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert any Object to String?
提问by Hemant Koli
Here is my code:
这是我的代码:
for (String toEmail : toEmailList)
{
Log.i("GMail","toEmail: "+toEmail);
emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
}
Please give me some suggestion about this.
请给我一些关于这个的建议。
回答by Adarsh Yadav
I am not getting your question properly but as per your heading, you can convert any type of object to string by using toString()
function on a String
Object.
我没有正确回答您的问题,但根据您的标题,您可以通过toString()
在String
对象上使用函数将任何类型的对象转换为字符串。
回答by Salmaan
To convert any object to string there are several methods in Java
要将任何对象转换为字符串,Java 中有几种方法
String convertedToString = String.valueOf(Object); //method 1
String convertedToString = "" + Object; //method 2
String convertedToString = Object.toString(); //method 3
I would prefer the first and third
我更喜欢第一个和第三个
EDIT
If working in kotlin, the official android language
编辑
如果在 kotlin 中工作,官方的 android 语言
val number: Int = 12345
String convertAndAppendToString = "number = $number" //method 1
String convertObjectMemberToString = "number = ${Object.number}" //method 2
String convertedToString = Object.toString() //method 3
回答by Mahesh Kapoor
"toString()" is Very useful method which returns a string representation of an object. The "toString()" method returns a string reperentation an object.It is recommended that all subclasses override this method.
“toString()”是非常有用的方法,它返回对象的字符串表示。“toString()”方法返回一个字符串表示一个对象。建议所有子类都覆盖此方法。
Declaration: java.lang.Object.toString()
声明:java.lang.Object.toString()
Since, you have not mentioned which object you want to convert, so I am just using any object in sample code.
因为,您没有提到要转换哪个对象,所以我只是在示例代码中使用任何对象。
Integer integerObject = 5;
String convertedStringObject = integerObject .toString();
System.out.println(convertedStringObject );
You can find the complete code here. You can test the code here.
回答by white hills
I am try to convert Object type variable into string using this line of code
我尝试使用这行代码将对象类型变量转换为字符串
try this to convert object value to string:
试试这个将对象值转换为字符串:
Java Code
Java代码
Object dataobject=value;
//convert object into String
String convert= String.valueOf(dataobject);
回答by Bogdan Kobylynskyi
If the class does not have toString()
method, then you can use ToStringBuilder
class from org.apache.commons:commons-lang3
如果类没有toString()
方法,那么你可以使用ToStringBuilder
从类org.apache.commons:公地lang3
pom.xml:
pom.xml:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
code:
代码:
ToStringBuilder.reflectionToString(yourObject)
回答by saeedmpt
I've written a few methods for convertby Gsonlibrary and java 1.8.
thay are daynamic model for convert.
我已经编写了一些通过Gson库和java 1.8进行转换的方法。
他们是转换的daynamic模型。
string to object
字符串到对象
object to string
对象到字符串
List to string
列表到字符串
string to List
字符串到列表
HashMap to String
哈希映射到字符串
String to JsonObj
字符串到 JsonObj
//saeedmpt
public static String convertMapToString(Map<String, String> data) {
//convert Map to String
return new GsonBuilder().setPrettyPrinting().create().toJson(data);
}
public static <T> List<T> convertStringToList(String strListObj) {
//convert string json to object List
return new Gson().fromJson(strListObj, new TypeToken<List<Object>>() {
}.getType());
}
public static <T> T convertStringToObj(String strObj, Class<T> classOfT) {
//convert string json to object
return new Gson().fromJson(strObj, (Type) classOfT);
}
public static JsonObject convertStringToJsonObj(String strObj) {
//convert string json to object
return new Gson().fromJson(strObj, JsonObject.class);
}
public static <T> String convertListObjToString(List<T> listObj) {
//convert object list to string json for
return new Gson().toJson(listObj, new TypeToken<List<T>>() {
}.getType());
}
public static String convertObjToString(Object clsObj) {
//convert object to string json
String jsonSender = new Gson().toJson(clsObj, new TypeToken<Object>() {
}.getType());
return jsonSender;
}