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
136
votes
12 answers

Is there anyway to handy convert a dictionary to String?

I found the default implemtation of ToString in the dictionary is not what I want. I would like to have {key=value, ***}. Any handy way to get it?
user705414
  • 20,472
  • 39
  • 112
  • 155
128
votes
3 answers

What is the meaning of ToString("X2")?

I'm studying MD5 encryption, and have found this code using Google: public string CalculateMD5Hash(string input) { // Primeiro passo, calcular o MD5 hash a partir da string MD5 md5 = System.Security.Cryptography.MD5.Create(); byte[]…
Lai32290
  • 8,062
  • 19
  • 65
  • 99
127
votes
8 answers

Confused about __str__ on list in Python

Coming from a Java background, I understand that __str__ is something like a Python version of toString (while I do realize that Python is the older language). So, I have defined a little class along with an __str__ method as follows: class Node: …
Christofer Ohlsson
  • 3,097
  • 4
  • 39
  • 56
125
votes
4 answers

Swift equivalent of Java toString()

What is the Swift equivalent of Java toString() to print the state of a class instance?
Marcus Leon
  • 55,199
  • 118
  • 297
  • 429
123
votes
5 answers

Why does 0.ToString("#.##") return an empty string instead of 0.00 or at least 0?

Why does 0.ToString("#.##") return an empty string? Shouldn't it be 0.00 or at least 0?
Imran Qadir Baksh - Baloch
  • 32,612
  • 68
  • 179
  • 322
122
votes
2 answers

What is this: [Ljava.lang.Object;?

I get this when I call toString on an object I received from a function call. I know the type of the object is encoded in this string, but I don't know how to read it. What is this type of encoding called?
Landon Kuhn
  • 76,451
  • 45
  • 104
  • 130
110
votes
26 answers

Convert a vector to a string

I have a vector container that has integers (e.g. {1,2,3,4}) and I would like to convert to a string of the form "1,2,3,4" What is the cleanest way to do that in C++? In Python this is how I would do it: >>> array = [1,2,3,4] >>>…
D R
  • 21,936
  • 38
  • 112
  • 149
106
votes
2 answers

Map to String in Java

When I do System.out.println(map) in Java, I get a nice output in stdout. How can I obtain this same string representation of a Map in a variable without meddling with standard output? Something like String mapAsString = Collections.toString(map)?
Dan
  • 11,077
  • 20
  • 84
  • 119
100
votes
7 answers

C# debugging: [DebuggerDisplay] or ToString()?

There are two ways to increase the usefulness of debugging information instead of seeing {MyNamespace.MyProject.MyClass} in the debugger. These are the use of DebuggerDisplayAttribute and the ToString() method. using…
bwerks
  • 8,651
  • 14
  • 68
  • 100
92
votes
5 answers

Join an array by a comma and a space

I have an array that I want converted to a comma delimited string. Array.toString() works, but if I have a rather large array it won't wrap because there are no spaces after the commas: document.body.innerHTML =…
Myles Gray
  • 8,711
  • 7
  • 48
  • 70
90
votes
2 answers

TypeScript override ToString()

Let's say I have a class Person which looks like this: class Person { constructor( public firstName: string, public lastName: string, public age: number ) {} } I have overridden the toString method as follows. public…
Duncan Lukkenaer
  • 12,050
  • 13
  • 64
  • 97
87
votes
7 answers

How to change symbol for decimal point in double.ToString()?

I would like to change decimal point to another character in C#. I have a double variable value double value; and when I use the command: Console.WriteLine(value.ToString()); // output is 1,25 I know I can do…
MartyIX
  • 27,828
  • 29
  • 136
  • 207
77
votes
9 answers

Difference between .ToString and "as string" in C#

What is the difference between using the two following statements? It appears to me that the first "as string" is a type cast, while the second ToString is an actual call to a method that converts the input to a string? Just looking for some insight…
jaywon
  • 8,164
  • 10
  • 39
  • 47
71
votes
6 answers

Negative numbers to binary string in JavaScript

Anyone knows why javascript Number.toString function does not represents negative numbers correctly? //If you try (-3).toString(2); //shows "-11" // but if you fake a bit shift operation it works as expected (-3 >>> 0).toString(2); // print…
fernandosavio
  • 9,849
  • 4
  • 24
  • 34
70
votes
10 answers

Dumping a java object's properties

Is there a library that will recursively dump/print an objects properties? I'm looking for something similar to the console.dir() function in Firebug. I'm aware of the commons-lang ReflectionToStringBuilder but it does not recurse into an object.…
Kevin
  • 30,111
  • 9
  • 76
  • 83