Explicit vs implicit call of toString

There’s little difference. Use the one that’s shorter and works more often. If you actually want to get the string value of an object for other reasons, and want it to be null friendly, do this: String s = String.valueOf(obj); Edit: The question was extended, so I’ll extend my answer. In both cases, they compile … Read more

How to create an extension method for ToString?

Extension methods are only checked if there are no applicable candidate methods that match. In the case of a call to ToString() there will always be an applicable candidate method, namely, the ToString() on object. The purpose of extension methods is to extend the set of methods available on a type, not to override existing … Read more

Convert double to string C++? [duplicate]

You can’t do it directly. There are a number of ways to do it: Use a std::stringstream: std::ostringstream s; s << “(” << c1 << “, ” << c2 << “)”; storedCorrect[count] = s.str() Use boost::lexical_cast: storedCorrect[count] = “(” + boost::lexical_cast<std::string>(c1) + “, ” + boost::lexical_cast<std::string>(c2) + “)”; Use std::snprintf: char buffer[256]; // make sure … Read more

Reverse (parse the output) of Arrays.toString(int[]) [duplicate]

Pretty easy to just do it yourself: public class Test { public static void main(String args[]){ int[] i = fromString(Arrays.toString(new int[] { 1, 2, 3} )); } private static int[] fromString(String string) { String[] strings = string.replace(“[“, “”).replace(“]”, “”).split(“, “); int result[] = new int[strings.length]; for (int i = 0; i < result.length; i++) { … Read more

Why is a round-trip conversion via a string not safe for a double?

I found the bug. .NET does the following in clr\src\vm\comnumber.cpp: DoubleToNumber(value, DOUBLE_PRECISION, &number); if (number.scale == (int) SCALE_NAN) { gc.refRetVal = gc.numfmt->sNaN; goto lExit; } if (number.scale == SCALE_INF) { gc.refRetVal = (number.sign? gc.numfmt->sNegativeInfinity: gc.numfmt->sPositiveInfinity); goto lExit; } NumberToDouble(&number, &dTest); if (dTest == value) { gc.refRetVal = NumberToString(&number, ‘G’, DOUBLE_PRECISION, gc.numfmt); goto lExit; } DoubleToNumber(value, … Read more

Dumping a java object’s properties

You could try XStream. XStream xstream = new XStream(new Sun14ReflectionProvider( new FieldDictionary(new ImmutableFieldKeySorter())), new DomDriver(“utf-8”)); System.out.println(xstream.toXML(new Outer())); prints out: <foo.ToString_-Outer> <intValue>5</intValue> <innerValue> <stringValue>foo</stringValue> </innerValue> </foo.ToString_-Outer> You could also output in JSON And be careful of circular references 😉

How do I automatically display all properties of a class and their values in a string? [duplicate]

I think you can use a little reflection here. Take a look at Type.GetProperties(). public override string ToString() { return GetType().GetProperties() .Select(info => (info.Name, Value: info.GetValue(this, null) ?? “(null)”)) .Aggregate( new StringBuilder(), (sb, pair) => sb.AppendLine($”{pair.Name}: {pair.Value}”), sb => sb.ToString()); }

How to make JSON.Net serializer to call ToString() when serializing a particular type?

You can do this easily with a custom JsonConverter: public class ToStringJsonConverter : JsonConverter { public override bool CanConvert(Type objectType) { return true; } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { writer.WriteValue(value.ToString()); } public override bool CanRead { get { return false; } } public override object ReadJson(JsonReader reader, Type objectType, object … Read more