tostring

Most efficient Dictionary<K,V>.ToString() with formatting?

大兔子大兔子 提交于 2019-11-29 06:30:39
What's the most efficient way to convert a Dictionary to a formatted string. e.g.: My method: public string DictToString(Dictionary<string, string> items, string format){ format = String.IsNullOrEmpty(format) ? "{0}='{1}' " : format; string itemString = ""; foreach(var item in items){ itemString = itemString + String.Format(format,item.Key,item.Value); } return itemString; } Is there a better/more concise/more efficient way? Note: the Dictionary will have at most 10 items and I'm not committed to using it if another similar "key-value pair" object type exists Also, since I'm returning strings

Get Command Prompt Output to String In Java

核能气质少年 提交于 2019-11-29 04:22:27
I need a java method that will read command prompt output and store it into a String to be read into Java. This is what I have so far but isn't working right. public void testGetOutput() { System.out.println("\n\n****This is the testGetOutput Method!****"); String s = null; String query = "dir " + this.desktop; try { Runtime runtime = Runtime.getRuntime(); InputStream input = runtime.exec("cmd /c " + query).getInputStream(); BufferedInputStream buffer = new BufferedInputStream(input); BufferedReader commandResult = new BufferedReader(new InputStreamReader(buffer)); String line = ""; try {

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

馋奶兔 提交于 2019-11-29 04:05:19
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, 16.32, 16.32, 17.05, 17.05, 17.05, 17.05, 18.29, 18.29, 19.16] The following do not work: Arrays.asList

Most efficient way to prevent an infinite recursion in toString()?

只谈情不闲聊 提交于 2019-11-29 01:26:14
To string on a collection can get into a infinite loop if somewhere in the graph of collected items is a reference back to itself. See example below. Yes, good coding practices should prevent this in the first place, but anyway, my question is: What is the most efficient way to detect a recursion in this situation? One approach is to use a set in a threadlocal, but that seems a bit heavy. public class AntiRecusionList<E> extends ArrayList<E> { @Override public String toString() { if ( /* ???? test if "this" has been seen before */ ) { return "{skipping recursion}"; } else { return super

'No overload for method 'ToString' takes '1' arguments' error occur While Formatting Datetime Field To String ON “dd-MM-yyyy” format

笑着哭i 提交于 2019-11-29 01:11:21
I have been working on asp.net 3.5.I want to Convert a DateTime Data from sqldatareader to a String on "dd-MM-yyyy" Format. But when I use "dd-MM-yyyy" formatting parameter as "rdMonthlyLeave["LEAVE_DATE"].ToString("dd-MM-yyyy")" browser returns compile error as Compiler Error Message: CS1501: No overload for method 'ToString' takes '1' arguments Do you have a solution? You need to cast it to DateTime first: DateTime leave = (DateTime) rdMonthlyLeave["LEAVE_DATE"]; DoSomethingWith(leave.ToString("dd-MM-yyyy")); or just ((DateTime)rdMonthlyLeave["LEAVE_DATE"]).ToString("dd-MM-yyyy") The return

Checking session if empty or not

六眼飞鱼酱① 提交于 2019-11-29 00:39:39
问题 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 if(Session["emp_num"] != null) { // The code } because sometimes when i check only with: if (!string.IsNullOrEmpty(Session["emp_num"].ToString())) { //The code } I face the following exception: Null Reference exception 回答1: Use this if the session variable emp_num will store a string: if (!string

datetime.tostring month and day language

半城伤御伤魂 提交于 2019-11-28 23:24:12
i have a list of email addresses of people that have different nationalities (for each person i have the iso code) when i send the email to all these people, in the text of the mail i need to to convert a datetime field to a string formatted in their specific culture. for this i'm doing CultureInfo ci = new CultureInfo(ISO); myStringDate = myDate.ToString(ci.DateTimeFormat.ShortDatePattern); and work perfect, but if i use LongDatePattern instead short, for displaying date like "Monday, 13 June 2010" its work fine except the language of the day and month. if the person culture is it-IT i need

Overriding ToString() of List<MyClass>

烈酒焚心 提交于 2019-11-28 21:17:06
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() { return Property1.ToString() + "-" + Property2.ToString(); } } I would like to have the following: var list = new List<MyClass> { new MyClass { Property1 = "A", Property2 = 1 }, new MyClass { Property1 = "Z", Property2 = 2 }, }; Console.WriteLine(list.ToString()); /* prints: A-1,Z-2 */ Is it possible to do so? Or I would have to subclass List<MyClass> to override the

Can Javascript get a function as text?

假如想象 提交于 2019-11-28 21:15:16
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? Nick Craver Updated to include caveats in the comments below from CMS , Tim Down , MooGoo : The closest thing available to what you're after is calling .toString() on a function to get the full function text, like this: function derp() { a(); b(); c(); } alert(derp.toString()); //"function derp() { a(); b(); c(); }" You can give it a try here , some caveats to be aware of though: The

Calling toString on a javascript function returns source code

南笙酒味 提交于 2019-11-28 21:06:58
I just found out that when you call toString() on a javascript function, as in myFunction.toString() , the source code of that function is returned. If you try it in the Firebug or Chrome console it will even go as far as formatting it nicely for you, even for minimized javascript files. I don't know what is does for obfuscated files. What's the use of such a toString implementation? HoLyVieR It has some use for debugging, since it lets you see the code of the function. You can check if a function has been overwritten, and if a variable points to the right function. It has some uses for