How to convert any Object to String?
Asked Answered
C

7

43

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.

Cantatrice answered 6/8, 2015 at 4:50 Comment(1)
Can you please clarify your question, which object?Bandung
B
77

To convert any object to string there are several methods in 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

val number: Int = 12345
String convertAndAppendToString = "number = $number"   //method 1

String convertObjectMemberToString = "number = ${Object.number}" //method 2

String convertedToString = Object.toString()  //method 3
Bandung answered 6/8, 2015 at 5:42 Comment(3)
Object.toString() can throws NPEWarplane
String.valueOf ends up calling toString() anyway, but it does do a null check.Queer
Completely forgot about String.valueOf()Grafting
E
9

If the class does not have toString() method, then you can use ToStringBuilder class from org.apache.commons:commons-lang3

pom.xml:

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
  <version>3.12</version>
</dependency>

code:

ToStringBuilder.reflectionToString(yourObject)
Eridanus answered 1/9, 2019 at 17:16 Comment(0)
P
5

I've written a few methods for convert by Gson library and java 1.8 .
thay are daynamic model for convert.

string to object

object to string

List to string

string to List

HashMap to String

String to 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;
}
Purpura answered 31/12, 2019 at 14:5 Comment(0)
K
4

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

Declaration: 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.

Khaki answered 8/8, 2015 at 8:57 Comment(0)
C
3

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

 Object dataobject=value;
    //convert object into String  
     String convert= String.valueOf(dataobject);
Chesnut answered 22/1, 2019 at 16:17 Comment(0)
S
0

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.

Salable answered 6/8, 2015 at 5:31 Comment(0)
S
0

try this one

 ObjectMapper mapper = new ObjectMapper();
 String oldSerial = mapper.writeValueAsString(list);
Sanitary answered 10/8, 2022 at 1:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.