tostring

Map to String in Java

天大地大妈咪最大 提交于 2019-11-27 02:29:45
问题 When I do System.out.println(map) in Java, I get a nice output in stdout. How can I obtain this same string representation of a Map in a variable without meddling with standard output? Something like String mapAsString = Collections.toString(map) ? 回答1: Use Object#toString() . String string = map.toString(); That's after all also what System.out.println(object) does under the hoods. The format for maps is described in AbstractMap#toString(). 回答2: You can also use google-collections (guava)

C# ToString()方法

荒凉一梦 提交于 2019-11-27 01:28:38
ToString的设计目的:为对象提供一个字符串,该字符串描述当前对象的信息。 1、Object的ToString 返回当前对象所属类型的全名,这个意义不大,但是设计很合理,因为有些对象没法返回一个有意义的字符串说明,比如FileStream或者HashTable。 2、Object 是所有类的根,所有类都直接或者间接继承Object。这些类大部分都重写了ToString方法。特别是值类型,比如int,double,值类型重写ToString,将实例的数值转换为等效的字符串。 转载于:https://www.cnblogs.com/nzbbody/archive/2012/01/10/2318030.html 来源: https://blog.csdn.net/weixin_30535913/article/details/99234356

How to display DateTime with an abbreviated Time Zone?

淺唱寂寞╮ 提交于 2019-11-27 01:28:37
问题 I am aware of the System.TimeZone class as well as the many uses of the DateTime.ToString() method. What I haven't been able to find is a way to convert a DateTime to a string that, in addition to the time and date info, contains the three-letter Time Zone abbreviation (in fact, much the same way StackOverflow's tooltips for relative time display works). To make an example easy for everyone to follow as well as consume, let's continue with the StackOverflow example. If you look at the tooltip

How do I automatically display all properties of a class and their values in a string? [duplicate]

拜拜、爱过 提交于 2019-11-27 01:17:36
This question already has an answer here: C#: Printing all properties of an object [duplicate] 9 answers Imagine a class with many public properties. For some reason, it is impossible to refactor this class into smaller subclasses. I'd like to add a ToString override that returns something along the lines of: Property 1: Value of property 1\n Property 2: Value of property 2\n ... Is there a way to do this? I think you can use a little reflection here. Take a look at Type.GetProperties() . private PropertyInfo[] _PropertyInfos = null; public override string ToString() { if(_PropertyInfos ==

Convert double to string C++? [duplicate]

人盡茶涼 提交于 2019-11-27 00:33:08
Possible Duplicate: How do I convert a double into a string in C++? I want to combine a string and a double and g++ is throwing this error: main.cpp: In function ‘int main()’: main.cpp:40: error: invalid operands of types ‘const char [2]’ and ‘double’ to binary ‘operator+’ Here is the line of code which it is throwing the error on: storedCorrect[count] = "("+c1+","+c2+")"; storedCorrect[] is a string array, and c1 and c2 are both doubles. Is there a way to convert c1 and c2 to strings to allow my program to compile correctly? You can't do it directly. There are a number of ways to do it: Use a

Android: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference

倖福魔咒の 提交于 2019-11-26 23:35:06
问题 Facing a problem with a practice app I'm working on. I'm facing a NullPointerException problem relating to the toString method. Being new to android app development, I'm unsure of the exact cause even after my research into this. Hence I ask for someone who is more familiar with the stack trace to kindly help me out. Note: The error occurs when I click on the listview entry to access an edit page for the diary entry. However it doesn't seem to go to the edit page at all. Below you'll find my

Is it possible in Java to override 'toString' for an Objects array?

南楼画角 提交于 2019-11-26 22:41:19
Is it possible in Java to override a toString for an Objects array? For example, let's say I created a simple class, User (it doesn't really matter which class is it since this is a general question). Is it possible that, once the client creates a User[] array and the client uses System.out.print(array) , it won't print the array's address but instead a customized toString() ? PS: of course I can't just override toString() in my class since it's related to single instances. No. Of course you can create a static method User.toString( User[] ), but it won't be called implicitly. You can use

How does ToString on an anonymous type work?

梦想与她 提交于 2019-11-26 21:28:50
问题 I was messing with anonymous types, and I accidentally outputted it onto the console. It looked basically how I defined it. Here's a short program that reproduces it: using System; class Program { public static void Main(string[] args) { int Integer = 2; DateTime DateTime = DateTime.Now; Console.WriteLine(new { Test = 0, Integer, s = DateTime }); Console.ReadKey(true); } } Now, the output is: { Test = 0, Integer = 2, s = 28/05/2013 15:07:19 } I tried using dotPeek to get into the assembly to

Dumping a java object's properties

我怕爱的太早我们不能终老 提交于 2019-11-26 21:24:55
Is there a library that will recursively dump/print an objects properties? I'm looking for something similar to the console.dir() function in Firebug. I'm aware of the commons-lang ReflectionToStringBuilder but it does not recurse into an object. I.e., if I run the following: public class ToString { public static void main(String [] args) { System.out.println(ReflectionToStringBuilder.toString(new Outer(), ToStringStyle.MULTI_LINE_STYLE)); } private static class Outer { private int intValue = 5; private Inner innerValue = new Inner(); } private static class Inner { private String stringValue =

BigInteger to Hex/Decimal/Octal/Binary strings?

好久不见. 提交于 2019-11-26 20:33:01
In Java, I could do BigInteger b = new BigInteger(500); Then format it as I pleased b.toString(2); //binary b.toString(8); //octal b.toString(10); //decimal b.toString(16); //hexadecimal In C#, I can do int num = int.Parse(b.ToString()); Convert.ToString(num,2) //binary Convert.ToString(num,8) //octal etc. But I can only do it with long values and smaller. Is there some method to print a BigInteger with a specified base? I posted this, BigInteger Parse Octal String? , yesterday and received the solution of how to convert basically all strings to BigInteger values, but haven't had success