Converting an Object to a String

Convert Object to String in java

As you've noticed you get the string "null" instead of an actual null value. This is by design:

Returns:
if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.

That said, you could just check if it's null before converting it:

import java.math.BigDecimal;

class Main
{
public static void main (String[] args)
{
BigDecimal foo = new BigDecimal(1.0);
BigDecimal bar = null;

// if the value isn't NULL, use String.valueOf, else use NULL
String fooString = foo != null ? String.valueOf(foo) : null;
String barString = bar != null ? String.valueOf(bar) : null;

System.out.println(fooString == null ? "REALNULL" : "\"" + fooString + "\"");
System.out.println(barString == null ? "REALNULL" : "\"" + barString + "\"");
}
}

Output:

"1"
REALNULL

DEMO

How to convert object into string in javascript?

let toString = ({name, age, language}) => `name: ${name}, age: ${age}, language: ${language}`;

const david = { name: 'David', age: 22, language: 'PHP' };

console.log(toString(david));

How to convert object value into string containing multiple data in objects?

First, you need to override the toString() method in your Author class, to pretty print the fields. An example could be:

class Author {

// ... your fields ...

@Override
public String toString() {
return "Author{" +
"name='" + name + '\'' +
", email='" + email + '\'' +
", gender=" + gender +
'}';
}
}

Then, here's the mistake that you are making.
The toString() method from the Author class returns a String, not an array of String (String[] as you did). So, you could just do:

String authDetails = book.getAuthor().toString();
System.out.println(authDetails);

But, the recommended way to do this is the following. Just use an object for the Author class, and pass that object to the System.out.println(...) method. This will automatically call the toString() method on the given object.

Author author = book.getAuthor();
System.out.println(author);

Has the same effect as:

System.out.println(author.toString());

how to convert object to string in java

I'm afraid your map contains something other than String objects. If you call toString() on a String object, you obtain the string itself.

What you get [Ljava.lang.String indicates you might have a String array.

Convert Object to string and back

Here you are:

var object = {

"1": [1, 2, {

3: "3"

}]

};

var str = JSON.stringify(object);

console.log(str);

var obj = JSON.parse(str);

console.log(obj["1"][2][3]);

How to convert an object to a string?

When you use ... |Select-Object PropertyName, it produces an object with a property named PropertyName, copying the value from the corresponding property on the input item.

Use Select-Object -ExpandProperty PropertyName or ForEach-Object MemberName to get just the value of the property:

$a = Get-Item . | Select-Object -ExpandProperty Name 
# or
$a = Get-Item . | ForEach-Object Name

... or reference the property directly:

$a = (Get-Item .).Name


Related Topics



Leave a reply



Submit