tostring

Why does String(null) work?

久未见 提交于 2019-12-30 07:51:37
问题 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 Object.prototype.toString.call(null) . because that evaluates to [object Null] . [edit]: from the spec ECMA-262/5th edition (page 48). This doesn't add to clarification, I'd say: /* Table 13 — ToString Conversions ----------------------------------------------

toString() method within System.out.println() a double call?

强颜欢笑 提交于 2019-12-30 04:06:08
问题 A professor of mine once said that the following code should never be done: System.out.println(object.toString()); He said (and I believe cited "Effective Java") it causes a double call. Since the print statement calls the toString method of an object, it would be less efficient to have the toString method called twice. The preferred method would be to just use: System.out.println(object); Obviously this way looks better in code and would save time. I will always do it like this no matter

Using string representations of enum values in switch-case

夙愿已清 提交于 2019-12-30 03:58:06
问题 Why is it not possible to use enum values as strings in a switch case? (Or what is wrong with this:) String argument; switch (argument) { case MyEnum.VALUE1.toString(): // Isn't this equal to "VALUE1" ? // something break; case MyEnum.VALUE2.toString(): // something else break; 回答1: You can only use strings which are known at compile time. The compiler cannot determine the result of that expression. Perhaps you can try String argument = ... switch(MyEnum.valueOf(argument)) { case VALUE1: case

Create string from HTMLDivElement

◇◆丶佛笑我妖孽 提交于 2019-12-30 01:53:31
问题 What I would like to be able to do is create a string from a Javascript HTMLElement Object. For example: var day = document.createElement("div"); day.className = "day"; day.textContent = "Random Text"; Now we have create the day HTMLDivElement Object is it possible to make it print as a string? e.g. <div class="day">Random Text</div> 回答1: Variant on Gump's wrapper, since his implementation lifts the target node out of the document. function nodeToString ( node ) { var tmpNode = document

How convert TimeSpan to 24 hours and minutes String?

社会主义新天地 提交于 2019-12-30 01:39:59
问题 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? 回答1: myTimeSpan.ToString(@"hh\:mm") Custom TimeSpan Format Strings The custom TimeSpan format specifiers do not include placeholder separator symbols, such as the symbols that separate days from hours, hours from minutes, or seconds from fractional seconds. Instead, these symbols must be included in

包装类和toString和static关键字

爱⌒轻易说出口 提交于 2019-12-29 21:29:43
包装类   针对八种基本数据类型定义的引用类型。   有类的特点,可以调用类中的方法。 基本数据类型 包装类 boolean Boolean byte Byte short Short int Integer long Long char Character float Float double Double 基本数据类型包装成包装类的实例------装箱(类型要匹配)   通过包装类的构造方法实现:Integer t = new Integer(i);   通过字符串参数构造包装类对象:Float f = new Float("4.56"); 获得包装类对象中包装的基本类型变量-----拆箱(类型要匹配)   调用包装类的.xxxValue()方法:boolean b = bObj.booleanValue(); JDK1.5之后,支持自动装箱,自动拆箱。但类型必须匹配。如果类型不匹配编译不报错,但是运行会报错。   自动装箱:Integer A = 112;//直接赋值就是自动装箱   自动拆箱:int B = A;//将变量A的值赋值给基本数据类型B就是自动拆箱 字符串转换成基本数据类型   通过包装类的构造方法实现:int i = new Integer("12");   通过包装类的parseXxx(String s)静态方法实现:Float f = Float

How to create an array of non repeating random numbers

送分小仙女□ 提交于 2019-12-29 08:41:32
问题 I have a lottery application in C# which takes in the number of numbers to draw and also the maximum number to draw.I have coded up to creating an array holding the required random numbers but I need them to be unique and am having trouble doing it.I would be very grateful if someone could give me some advice on this,Thanks Here is my code so far: class Lottery { static int[] numberHolder; //array to be filled with numbers up to an //amount entered by the user eg 42 Max static int[]

How to create an array of non repeating random numbers

守給你的承諾、 提交于 2019-12-29 08:41:29
问题 I have a lottery application in C# which takes in the number of numbers to draw and also the maximum number to draw.I have coded up to creating an array holding the required random numbers but I need them to be unique and am having trouble doing it.I would be very grateful if someone could give me some advice on this,Thanks Here is my code so far: class Lottery { static int[] numberHolder; //array to be filled with numbers up to an //amount entered by the user eg 42 Max static int[]

Why can't I use a .ToString() method on a char array in C#?

最后都变了- 提交于 2019-12-29 08:09:45
问题 I am working through an exercise where I am supposed to ask the user for a string, then reverse the string and print it out to the console. I wrote this: Console.WriteLine("Please enter a string of characters: "); char[] words = Console.ReadLine().ToCharArray(); Console.WriteLine("You entered {0}, the reverse of which is {1}", words.ToString(), words.Reverse().ToString()); Which did not work. Apparently sending a char array to .ToString() gives back "System.Char[]" and reversing it gives back

convert ArrayList.toString() back to ArrayList in one call

送分小仙女□ 提交于 2019-12-29 06:15:07
问题 I have a toString() representation of an ArrayList . Copying the toString() value to clipboard, I want to copy it back into my IDE editor, and create the ArrayList instance in one line. In fact, what I'm really doing is this: my ArrayList.toString() has data I need to setup a unit test. I want to copy this ArrayList.toString() into my editor to build a test against this edge case I don't want to parse anything by hand My input looks like this: [15.82, 15.870000000000001, 15.92, 16.32, 16.32,