Questions tagged [tostring]

"toString" or "ToString" is a major formatting method or function used in high level programming languages. It converts an Object to its string representation so that it is suitable for display.

toString() is a function which can be overridden.

toString

The toString() method is widely used to represent objects as human-readable text and practically every class should implement it. Usually it is very simple: generated string contains the object type and lists values of the most important fields. That's why process of writing such methods can easilly be automated. Eclipse, as the best java IDE in the world, should include functionality of automatic toString() method generation to make its users lives even simpler. Implementing it is the aim of this project.

Java represents this as the toString() method where C# represents it as ToString().

2528 questions
64
votes
3 answers

How to create an extension method for ToString?

I have tried this: public static class ListHelper { public static string ToString(this IList list) { return string.Join(", ", list.ToArray()); } public static string ToString(this String[] array) { …
IAdapter
  • 62,595
  • 73
  • 179
  • 242
64
votes
12 answers

toString(), equals(), and hashCode() in an interface

So, I have an interface with a bunch of methods that need to be implemented, the method names are irrelevant. The objects that implement this interface are often put into collections, and also have a special toString() format that I want them to…
J_Y_C
  • 697
  • 1
  • 5
  • 7
61
votes
7 answers

Java toString() using reflection?

I was writing a toString() for a class in Java the other day by manually writing out each element of the class to a String and it occurred to me that using reflection it might be possible to create a generic toString() method that could work on ALL…
James McMahon
  • 48,506
  • 64
  • 207
  • 283
57
votes
3 answers

Set up dot instead of comma in numeric values

I have new XmlDocument object, i.g. xml is created during my program... I want all numeric values in created xml was with dot symbol instead of comma by default. Can I do something to declare it once, not to parse every decimal value? I.e. To set up…
Ksice
  • 3,277
  • 9
  • 43
  • 67
56
votes
6 answers

Using Google Guava's Objects.ToStringHelper

I used ToStringBuilder.reflectionToString(class) in commons-lang, to implement toString() for simple DTOs. Now I'm trying to use Google Guava instead of Apache commons library. And I found Objects.ToStringHelper in Guava. But it's too verbose if…
philipjkim
  • 3,999
  • 7
  • 35
  • 48
55
votes
11 answers

.NET: How to convert Exception to string?

When an exception is thrown (while debugging in the IDE), i have the opportunity to view details of the exception: But in code if i call exception.ToString() i do not get to see those useful details: System.Data.SqlClient.SqlException (0x80131904):…
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
50
votes
11 answers

How do I format a C# decimal to remove extra following 0's?

I want to format a string as a decimal, but the decimal contains some following zeros after the decimal. How do I format it such that those meaningless 0's disappear? string.Format("{0}", 1100M); string.Format("{0}", 1100.1M); string.Format("{0}",…
Scott Stafford
  • 43,764
  • 28
  • 129
  • 177
49
votes
6 answers

How do I format a number with commas?

int a = 10000000; a.ToString(); How do I make the output? 10,000,000
ssl
48
votes
4 answers

Is specialization of std::to_string for custom types allowed by the C++ standard?

In C++11 and later, is it allowed to specialize std::to_string in the std namespace for custom types? namespace std { string to_string(::MyClass const & c) { return c.toString(); } } Sample use-case: int main() { MyClass c; std::cout <<…
jotik
  • 17,044
  • 13
  • 58
  • 123
46
votes
5 answers

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

Imagine a class with many public properties. For some reason, it is impossible to refactor this class into smaller subclasses. I'd like to add a ToString override that returns something along the lines of: Property 1: Value of property 1\n Property…
mafu
  • 31,798
  • 42
  • 154
  • 247
46
votes
5 answers

Laravel Error: Method Illuminate\View\View::__toString() must not throw an exception

Have you seen this lovely error while working in Laravel? Method Illuminate\View\View::__toString() must not throw an exception I have seen it and it's incredibly annoying. I have found out two reasons why this error gets thrown. I just want to…
cbloss793
  • 1,555
  • 4
  • 19
  • 30
45
votes
8 answers

Making a user-defined class std::to_string-able

I know it seems too much Java or C#. However, is it possible/good/wise to make my own class valid as an input for the function std::to_string ? Example: class my_class{ public: std::string give_me_a_string_of_you() const{ return "I am " +…
Humam Helfawi
  • 19,566
  • 15
  • 85
  • 160
44
votes
10 answers

check if item can be converted to string?

I am writing a debug method. What I have is if(is_xxx($item)){ //echo output info for type } what I want to do at the end is if(can_be_string($item)) echo $item; Is there a can_be_string type function?
Hailwood
  • 89,623
  • 107
  • 270
  • 423
44
votes
4 answers

toString override in C++

In Java, when a class overrides .toString() and you do System.out.println() it will use that. class MyObj { public String toString() { return "Hi"; } } ... x = new MyObj(); System.out.println(x); // prints Hi How can I accomplish that in C++,…
Aillyn
  • 23,354
  • 24
  • 59
  • 84
43
votes
6 answers

Kotlin - generate toString() for a non-data class

Situation: I have a class with lateinit fields, so they are not present in the constructor: class ConfirmRequest() { lateinit var playerId: String } I'd like to have a toString() method with all fields and don't want to write it manually, to…
awfun
  • 2,316
  • 4
  • 31
  • 52