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
43
votes
8 answers

What is the best standard style for a toString implementation?

We have a lot of objects for which we like to implement a simple toString to output attributes of the object. Some of these attributes may be complex objects themselves. Is there any standard, or simply just a best practice for a style? I'm…
Nicole
  • 32,841
  • 11
  • 75
  • 101
43
votes
5 answers

Explicit vs implicit call of toString

I used to use the implicit call of toString when wanting some debug info about an object, because in case of the object is null it does not throw an Exception. For instance: System.out.println("obj: "+obj); instead of: System.out.println("obj:…
Burkhard
  • 14,596
  • 22
  • 87
  • 108
43
votes
6 answers

What to import to use IOUtils.toString()?

I am trying to use IOUtils.toString() to read from a file. However, I am getting an error saying "IOUtils cannot be resolved." What am I supposed to be importing to allow me to use this function? String everything =…
user2380101
  • 431
  • 1
  • 4
  • 4
42
votes
8 answers

How to display DateTime with an abbreviated Time Zone?

I am aware of the System.TimeZone class as well as the many uses of the DateTime.ToString() method. What I haven't been able to find is a way to convert a DateTime to a string that, in addition to the time and date info, contains the three-letter…
Sean Hanley
  • 5,677
  • 7
  • 42
  • 53
41
votes
5 answers

How convert TimeSpan to 24 hours and minutes String?

I use this code for converting Timespan to String (for ex: 14:53) : myTimeSpan.ToString("hh:mm"); but this error occurs: Input string was not in a correct format What is the proper way to do this?
Majid
  • 13,853
  • 15
  • 77
  • 113
41
votes
24 answers

When is it desired to not implement toString() in Java?

A lead developer on my project has taken to referring to the project's toString() implementations as "pure cruft" and is looking to remove them from the code base. I've said that doing so would mean that any clients wishing to display the objects…
Allain Lalonde
  • 91,574
  • 70
  • 187
  • 238
40
votes
6 answers

Calling toString on a javascript function returns source code

I just found out that when you call toString() on a javascript function, as in myFunction.toString(), the source code of that function is returned. If you try it in the Firebug or Chrome console it will even go as far as formatting it nicely for…
Sergi Papaseit
  • 15,999
  • 16
  • 67
  • 101
38
votes
1 answer

StringBuilder.ToString() throw an 'Index out of range' Exception

I would really appreciate someone help me resolving the following issue: I am getting now and then the following exception: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: chunkLength on…
Gerhard Powell
  • 5,965
  • 5
  • 48
  • 59
37
votes
6 answers

C# int ToString format on 2 char int?

How do I use the ToString method on an integer to display a 2-char int i = 1; i.ToString() -> "01" instead of "1" Thanks.
Ray
  • 12,101
  • 27
  • 95
  • 137
37
votes
9 answers

Why does the toString method in java not seem to work for an array

I want to convert a character array to a string object using the toString() method in java. Here is a snippet of the test code I used: import java.util.Arrays; class toString{ public static void main(String[] args){ char[] Array = {'a',…
sidharth sharma
  • 3,025
  • 6
  • 23
  • 20
37
votes
11 answers

Where and why do we use __toString() in PHP?

I understand how it works but why would we practically use this? Isn't this the same as…
Stann
  • 13,518
  • 19
  • 65
  • 73
37
votes
3 answers

What does it mean when there is a number parameter passed to toString?

I'm just wondering what it means to attach a number as a parameter to the toString() method E.g. obj.toString(10); I googled and i have never seen a parameter before.
Dennis D
  • 1,293
  • 4
  • 17
  • 24
36
votes
10 answers

ToString on null string

Why does the second one of these produce an exception while the first one doesn't? string s = null; MessageBox.Show(s); MessageBox.Show(s.ToString()); Updated - the exception I can understand, the puzzling bit (to me) is why the first part doesn't…
MartW
  • 12,348
  • 3
  • 44
  • 68
36
votes
4 answers

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

I am using Newtonsoft.Json serializer to convert C# classes to JSON. For some classes I don't need the serializer to an instance to individual properties, but instead just call ToString on the object, i.e. public class Person { public string…
Vagif Abilov
  • 9,835
  • 8
  • 55
  • 100
35
votes
8 answers

C#: Overriding ToString() method for custom exceptions

I have a custom exception class which contains some additional fields. I want these to be written out in the ToString() method, but if I implement my own ToString(), I loose some other useful stuff (like writing the exception type name, the inner…
Igor Brejc
  • 18,714
  • 13
  • 76
  • 95