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
2386
votes
37 answers

What's the simplest way to print a Java array?

In Java, arrays don't override toString(), so if you try to print one directly, you get the className + '@' + the hex of the hashCode of the array, as defined by Object.toString(): int[] intArray = new int[] {1, 2, 3, 4,…
Alex Spurling
  • 54,094
  • 23
  • 70
  • 76
1700
votes
32 answers

How can I convert a stack trace to a string?

What is the easiest way to convert the result of Throwable.getStackTrace() to a string that depicts the stacktrace?
ripper234
  • 222,824
  • 274
  • 634
  • 905
1192
votes
42 answers

Converting an object to a string

How can I convert a JavaScript object into a string? Example: var o = {a:1, b:2} console.log(o) console.log('Item: ' + o) Output: Object { a=1, b=2} // very nice readable output :) Item: [object Object] // no idea what's inside :(
user680174
  • 11,921
  • 3
  • 15
  • 3
407
votes
14 answers

How do I print my Java object without getting "SomeType@2f92e0f4"?

I have a class defined as follows: public class Person { private String name; // constructor and getter/setter omitted } I tried to print an instance of my class: System.out.println(myPerson); but I got the following output:…
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
365
votes
26 answers

Enum ToString with user friendly strings

My enum consists of the following values: private enum PublishStatusses{ NotCompleted, Completed, Error }; I want to be able to output these values in a user friendly way though. I don't need to be able to go from string to value again.…
Boris Callens
  • 90,659
  • 85
  • 207
  • 305
323
votes
17 answers

How to get the entire document HTML as a string?

Is there a way in JS to get the entire HTML within the html tags, as a string? document.documentElement.??
Robin Rodricks
  • 110,798
  • 141
  • 398
  • 607
271
votes
14 answers

to_string is not a member of std, says g++ (mingw)

I am making a small vocabulary remembering program where words would would be flashed at me randomly for meanings. I want to use standard C++ library as Bjarne Stroustroup tells us, but I have encountered a seemingly strange problem right out of the…
Anurag Kalia
  • 4,668
  • 4
  • 21
  • 28
211
votes
15 answers

Problem with converting int to string in Linq to entities

var items = from c in contacts select new ListItem { Value = c.ContactId, //Cannot implicitly convert type 'int' (ContactId) to 'string' (Value). Text = c.Name }; var items = from c…
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
194
votes
3 answers

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

Recently I have had to serialize a double into text, and then get it back. The value seems to not be equivalent: double d1 = 0.84551240822557006; string s = d1.ToString("R"); double d2 = double.Parse(s); bool s1 = d1 == d2; // -> s1 is False But…
Philip Ding
  • 1,571
  • 2
  • 11
  • 11
176
votes
19 answers

Difference between Convert.ToString() and .ToString()

What is the difference between Convert.ToString() and .ToString()? I found many differences online, but what's the major difference?
Ayyappan Anbalagan
  • 11,022
  • 17
  • 64
  • 94
176
votes
6 answers

What is the Objective-C equivalent for "toString()", for use with NSLog?

Is there a method that I can override in my custom classes so that when NSLog(@"%@", myObject) is called, it will print the fields (or whatever I deem important) of my object? I guess I'm looking for the Objective-C equivalent of Java's…
George Armhold
  • 30,824
  • 50
  • 153
  • 232
172
votes
8 answers

How to convert an int array to String with toString method in Java

I am using trying to use the toString(int[]) method, but I think I am doing it wrong: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Arrays.html#toString(int[]) My code: int[] array = new int[lnr.getLineNumber() + 1]; int i =…
Jaanus
  • 16,161
  • 49
  • 147
  • 202
150
votes
7 answers

Override valueof() and toString() in Java enum

The values in my enum are words that need to have spaces in them, but enums can't have spaces in their values so it's all bunched up. I want to override toString() to add these spaces where I tell it to. I also want the enum to provide the correct…
WildBamaBoy
  • 2,663
  • 6
  • 25
  • 23
150
votes
7 answers

ToString() function in Go

The strings.Join function takes slices of strings only: s := []string{"foo", "bar", "baz"} fmt.Println(strings.Join(s, ", ")) But it would be nice to be able to pass arbitrary objects which implement a ToString() function. type ToStringConverter…
deamon
  • 89,107
  • 111
  • 320
  • 448
149
votes
16 answers

Is it possible to override JavaScript's toString() function to provide meaningful output for debugging?

When I console.log() an object in my JavaScript program, I just see the output [object Object], which is not very helpful in figuring out what object (or even what type of object) it is. In C# I'm used to overriding ToString() to be able to…
devios1
  • 36,899
  • 45
  • 162
  • 260
1
2 3
99 100