tostring

js笔记(强制类型转换)

微笑、不失礼 提交于 2020-01-09 16:42:55
这篇随笔记录一下js中数据的各种类型转换的规则,虽然很基础,但是重新过一遍会发现有些规范还是挺意想不到的 首先介绍一下ToString, ToNumber, ToBoolean 的转换规则 1、ToString    规则1:null 转换为 “null” , undefined 转换为 “undefined” , true 转换为 “true” ;   规则2:普通对象,除非自定义,否则用 toString();       数组的toString()方法,先把所有单元字符串化,然后再用“,”连接;[1,2,3,4] // “1,2,3,4”; 2、ToNumber   规则1:布尔值 true 转换为 1, false 转换为 0; undefined 转换为 NaN; null 转换为 0;字符串处理失败时返回NaN;   规则2:以 0开头地十六进制数会被当成十进制;   规则3:对象会先被处理成相应地基本类型值,再按照值类型做相应处理;        对象做ToPrimitive操作规则:先valueOf(), 如果valueOf的结果不是基本类型,再进行 toString() ;如果均不返回基本类型,则报TypeError;       使用Object.create(null) 创建的对象,无valueOf 跟 toString 方法,故不能被强制类型转换   规则4

Creating String representation of lambda expression [duplicate]

北城以北 提交于 2020-01-09 11:35:06
问题 This question already has answers here : Is it possible to retrieve lambda expression at runtime (2 answers) Closed 5 years ago . For debugging purposes I am trying to create string representations of lambda expressions (specifically of Predicate s, though it would be interesting for other lambda expressions too) in Java 8. My idea would be something like this: public class Whatever { private static <T> String predicateToString(Predicate<T> predicate) { String representation = ... // do magic

Entity Framework 4 / Linq: How to convert from DateTime to string in a query?

早过忘川 提交于 2020-01-09 07:15:16
问题 I have the following query: from a in Products select new ProductVM { id = a.id, modified = a.modified.ToString() } Which gives me an error of: LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression. The modified in the Products table is DateTime. The modified in the ProductVM class is string. Any ideas? This has to be a trivial issue. 回答1: ToString() is not supported in Linq to Entities - there is a list

Entity Framework 4 / Linq: How to convert from DateTime to string in a query?

别来无恙 提交于 2020-01-09 07:13:10
问题 I have the following query: from a in Products select new ProductVM { id = a.id, modified = a.modified.ToString() } Which gives me an error of: LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression. The modified in the Products table is DateTime. The modified in the ProductVM class is string. Any ideas? This has to be a trivial issue. 回答1: ToString() is not supported in Linq to Entities - there is a list

Why commenting/uncommenting alert() in constructor toggles variable as part of the obj instances

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-07 08:22:12
问题 In the following code, if I comment //alert("Your Name is: " +fname); in the constructor function then 'alert(p1.fname); alerts "Suresh" and If I remove the comment out the alert("Your Name is: " +fname); then browser console gives out the error : fname is not defined` function person () { this.fname = "Suresh"; alert("Your Name is: " +fname); } var p1 = new person(); alert(p1.fname); I am puzzled by this behaviour. Pls explain thanks 回答1: You're using a variable that doesn't exist in the

In Javascript, does console.log on an object call toString?

眉间皱痕 提交于 2020-01-06 07:58:08
问题 Array.prototype.toString = function(){ return "testing"; } a1 = [1,2,3]; console.log(a1); // returns [1,2,3] console.log(a1.toString()); // returns "testing" why is that? How does the first log print out values? I thought it used the toString method? 回答1: For Firefox(both Firebug and Dev Web Console), Chrome: No For IE: Yes 来源: https://stackoverflow.com/questions/13244308/in-javascript-does-console-log-on-an-object-call-tostring

Deleting all regex instances starting with char '[' and ending with char ']' from a String

社会主义新天地 提交于 2020-01-06 02:14:06
问题 I need to take a String and deleting all the regexes in it starting with character '[' and ending with character ']'. Now i don't know how to tackle this problem. I tried to convert the String to character array and then putting empty characters from any starting '[' till his closing ']' and then convert it back to a String using toString() method. MyCode: char[] lyricsArray = lyricsParagraphElements.get(1).text().toCharArray(); for (int i = 0;i < lyricsArray.length;i++) { if (lyricsArray[i]

C# Override ToString() and display in textBox

隐身守侯 提交于 2020-01-05 05:47:07
问题 I have this Class: namespace JimWcfFormTest3 { [DataContract] public class GateInfo { [DataMember] public int carid { get; set; } [DataMember] public int paid_at_gate { get; set; } [DataMember] public int wash_pkg_purch { get; set; } [DataMember] public string carte { get; set; } public override string ToString() { return "Car ID: " + carid + "Paid at Gate: " + paid_at_gate + "Wash Package: " + wash_pkg_purch + "Ala Carte: " + carte; } } } That is called by this WCF Service: namespace

dynamic.ToString() unexpected behaviour

家住魔仙堡 提交于 2020-01-03 21:04:23
问题 I'm wondering how does this code work: dynamic dynaString = 2; string b = dynaString.ToString(); When this one is not working: var list = new List<dynamic>(); var liststring = new List<string>(); liststring = list.Select(x => x.ToString()).ToList(); I know I can add Cast<string> after Select statement but that does not explain that behaviour. Why does ToString() on dynamic element work different when called on dynamic variable declared in code than on dynamic variable taken from list in LINQ.

Overriding ToString() and adding to ListBox C#

邮差的信 提交于 2020-01-02 02:34:51
问题 Can anyone explain this: public class Test : List<int> { public override string ToString() { return "My ToString"; } } If I instantiate this and add it to a ListBox control on a Windows Form , it displays "Collection" rather than "My ToString". Test test = new Test(); listBox1.Items.Add(test); I thought the add to Items would just call my class's ToString() . The following works as expected of course MessageBox.Show(test.ToString()); 回答1: For that to work you have to disable formatting: