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
34
votes
6 answers

Converting Range or DocumentFragment to string

Is there a way to get the html string of a JavaScript Range Object in W3C compliant browsers? For example, let us say the user selects the following: Hello World It is possible to get "Hello World" as a string using the Range.toString()…
SamGoody
  • 13,758
  • 9
  • 81
  • 91
34
votes
4 answers

Lua - Number to string behaviour

I would like to know how Lua handles the number to string conversions using the tostring() function. It is going to convert to an int (as string) if the number is round (i.e if number == (int) number) or is it always going to output a real (as…
Virus721
  • 8,061
  • 12
  • 67
  • 123
34
votes
2 answers

Can Javascript get a function as text?

Can Javascript get a function as text? I'm thinking like the inverse of eval(). function derp() { a(); b(); c(); } alert(derp.asString()); The result would be something like "a(); b(); c();" Does it exist?
Brandon
  • 411
  • 2
  • 5
  • 4
33
votes
6 answers

Checking session if empty or not

I want to check that session is null or empty i.e. some thing like this: if(Session["emp_num"] != null) { if (!string.IsNullOrEmpty(Session["emp_num"].ToString())) { //The code } } Or just …
Anyname Donotcare
  • 11,113
  • 66
  • 219
  • 392
33
votes
5 answers

BigInteger.toString method is deleting leading 0

I am trying to generate MD5 sum using MessageDigest. And i am having following code. byte[] md5sum = digest.digest(); BigInteger bigInt = new BigInteger(1, md5sum); output = bigInt.toString(16); This returns not 32 character string but a 31…
Dheeraj Joshi
  • 3,057
  • 8
  • 38
  • 55
32
votes
2 answers

C# Converting 20 digit precision double to string and back again

In C#. I have a double (which I've extracted from a database) that has 20 digit precision. In Visual Studio (using QuickWatch) I can see the value of the double to be = 0.00034101243963859839. I want to display this value in a textbox and then…
JohnM
30
votes
9 answers

What are the shortcut to Auto-generating toString Method in Eclipse?

Is it good or bad practice auto-generating toString methods for some simple classes? I was thinking of generating something like below where it takes the variable names and produces a toString method that prints the name followed by its…
Gordon
  • 4,823
  • 4
  • 38
  • 55
30
votes
7 answers

Overriding ToString() of List

I have a class MyClass, and I would like to override the method ToString() of instances of List: class MyClass { public string Property1 { get; set; } public int Property2 { get; set; } /* ... */ public override string ToString() …
Bruno Reis
  • 37,201
  • 11
  • 119
  • 156
29
votes
2 answers

how to add spaces between array items javascript

I am a beginner at javascript so I appreciate any help/advice given. My issue here is that I'm trying to figure out how to add space between items in the times[] array which holds the values for showtimes for each movie object. I tried to split at…
user1876829
  • 475
  • 1
  • 7
  • 12
28
votes
9 answers

How to 'cout' the correct number of decimal places of a double value?

I need help on keeping the precision of a double. If I assign a literal to a double, the actual value was truncated. int main() { double x = 7.40200133400; std::cout << x << "\n"; } For the above code snippet, the output was 7.402 Is there…
roxrook
  • 13,511
  • 40
  • 107
  • 156
28
votes
4 answers

Why does String(null) work?

null and undefined don't have a toString or valueOf method. Afaik using String calls the toString method of its parameter (e.g. String({}) => [object Object]). Why do String(null) or String(undefined work then? It doesn't implicitly do…
KooiInc
  • 119,216
  • 31
  • 141
  • 177
27
votes
4 answers

What is the difference between toString and mkString in scala?

I have a file that contains 10 lines - I want to retrieve it, and then split them with a newline("\n") delimiter. here's what I did val data = io.Source.fromFile("file.txt").toString; But this causes an error when I try to split the file on…
alan
  • 353
  • 2
  • 5
  • 9
27
votes
10 answers

Convert xml to string with jQuery

I'm loading an xml file with jQuery ajax loader, and need to convert it to a string so that I can save it out again using PHP post variables. What is the best way to do this?