tostring

String concatenation with Groovy

匿名 (未验证) 提交于 2019-12-03 02:44:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: What is the best (idiomatic) way to concatenate Strings in Groovy? Option 1: calculateAccountNumber ( bank , branch , checkDigit , account ) { bank + branch + checkDigit + account } Option 2: calculateAccountNumber ( bank , branch , checkDigit , account ) { "$bank$branch$checkDigit$account" } I've founded an interesting point about this topic in the old Groovy website: Things you can do but better leave undone. As in Java, you can concatenate Strings with the "+" symbol. But Java only needs that one of the two items of a "+"

How do I override ToString() and implement generic?

匿名 (未验证) 提交于 2019-12-03 02:38:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have code that I want to make the following changes: How do I override ToString()? It says: A static member ...ToString(System.Collections.Generic.List)' cannot be marked as override, virtual, or abstract. How do I make it generic? public static override string ToString(this List<int> list) { string output = ""; list.ForEach(item => output += item.ToString() + "," ); return output; } Thanks! 回答1: What are you trying to achieve? Often I want to output the contents of a list, so I created the following extension method: public static string

Convert QUrl with percent encoding into string

匿名 (未验证) 提交于 2019-12-03 02:18:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I use a URL entered by the user as text to initialize a QUrl object. Later I want to convert the QUrl back into a string for displaying it and to check it using regular expression. This works fine as long as the user does not enter any percent encoded URLs. Why doesn't the following example code work? qDebug() << QUrl("http://test.com/query?q=%2B%2Be%3Axyz%2Fen").toDisplayString(QUrl::FullyDecoded); It simply doesn't decode any of the percent-encoded characters. It should print "http://test.com/query?q=++e:xyz/en" but it actually prints

Why is the toString() method being called when I print an object?

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I can't seem to understand why when I use println method on the quarter object, it returns the value of the toString method. I never called the toString method why am I getting the return value? public class Main { public static void main(String[] args) { Quarter q = new Quarter(); Nickel n = new Nickel(); System.out.println(q); System.out.println(n); } } public abstract class Money { private int value; public Money(int v) { value=v; } public abstract int getValue(); protected int myValue() { return value; } public abstract String toString()

Create string from HTMLDivElement

天涯浪子 提交于 2019-12-03 02:08:41
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> Variant on Gump's wrapper, since his implementation lifts the target node out of the document. function nodeToString ( node ) { var tmpNode = document.createElement( "div" ); tmpNode.appendChild( node.cloneNode( true ) ); var str = tmpNode.innerHTML; tmpNode =

Generate Java Object from toString representation [duplicate]

匿名 (未验证) 提交于 2019-12-03 02:03:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: This question already has an answer here: Converting back from toString to Object 7 answers How to get back an object after performing .toString() on it? [duplicate] 4 answers We all know how to implement toString () method. It could be slightly custom implementation and different pattern how we print the object data. Using the generated toString , can we recreate the Object? I am not talking about Serialization here. Let me explain a scenario, You might have an application running happily in production and your log prints these objects when

non-static method toString() cannot be referenced from a static context

匿名 (未验证) 提交于 2019-12-03 01:57:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Don't want any code, just want some sort of guidance. Would like to keep my academic integrity in tact ;) I keep getting that annoying error. I need to call the toString method for each Room instance. Any suggestions? I would prefer an answer within 2 hours if at all possible. public class Hotel { //constant public static final int NUM_ROOMS = 20 ; //variables public Room [] theRoom ; public String name ; public int totalDays ; public double totalRate ; public int singleCount ; public int doubleCount ; public int roomsRented ;

ToString on Expression Trees produces badly formatted output

匿名 (未验证) 提交于 2019-12-03 01:22:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: When I use Expression.ToString() to convert an Expression Tree into human readable form, the result is something like this: x => (( x . ID > 2 ) OrElse ( x . ID != 6 )) x => (( x . ID > 2 ) AndAlso ( x . ID != 6 )) Ideally, I would want the output to show the operators instead of "OrElse" and "AndAlso": x => (( x . ID > 2 ) || ( x . ID != 6 )) x => (( x . ID > 2 ) && ( x . ID != 6 )) As a workaround, I could use the string.Replace() method.. . Replace ( "AndAlso" , "&&" ) . Replace ( "OrElse" , "||" ) but that has obvious

Marking ToString virtual in base class, what happens?

匿名 (未验证) 提交于 2019-12-03 00:58:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Consider the following (LinqPad) example. ToString in class X is marked virtual. Why is the output here not equal to "Hi, I'm Y, Hi, I'm X" but instead the typename is printed? Of course marking ToString virtual is wrong, because it is defined in Object as virtual, I am just trying to understand what is happening here. void Main() { Y y = new Y(); Console.WriteLine(y); } // Define other methods and classes here class X { public virtual String ToString() { return "Hi, I'm X"; } } class Y : X { public override String ToString() { return "Hi, I

__toString() must not throw an exception error when using string

匿名 (未验证) 提交于 2019-12-03 00:50:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am using Laravel 4 for a project I am working on. I need to retrieve the first comment from the post. I use the following code to do so. $comments = Comment::where('post_id', $post->id)->first(); This successfully retrieves the first comment (I know that because I print_r -ed $comments and it returned all the right information). However, the following line of code triggers the error __toString() must not throw an exception <td>{{$comments->content}}</td> When I print_r -ed that it returned type string, and returned the correct string as