tostring

How can I have toString() return a multi-line string?

旧时模样 提交于 2019-12-06 05:13:38
问题 I'm working on a program that searches through an array and finds the smallest value and then prints out the time, firstName, and lastName of the runner. What I need to figure out is how to return the three values on separate lines, something like: public String toString() { return String.format( firstName + " " + lastName + " " + Time ); } That's what I have right now Is there a way to have the three values print out on separate lines? 回答1: Try This public String toString(){ return String

String concatenation with the + symbol

时光总嘲笑我的痴心妄想 提交于 2019-12-06 03:43:36
Today I was reading Antonio's Blog about toString() performance and there is a paragraph: What used to be considered evil yesterday (“do not concatenate Strings with + !!!“), has become cool and efficient! Today the JVM compiles the + symbol into a string builder (in most cases) . So, do not hesitate, use it. Now I am confused, because he is saying Today the JVM compiles the + symbol into a string builder (in most cases) , but I have never heard or seen(code) anything like this before. Could someone please give example where JVM does this and in what conditions it happens ? The rule “do not

Why does Arrays.toString(values).trim() produce [bracketed text]?

半城伤御伤魂 提交于 2019-12-06 02:48:36
Map<String, String[]> map = request.getParameterMap(); for (Entry<String, String[]> entry : map.entrySet()) { String name = entry.getKey(); String[] values = entry.getValue(); String valuesStr = Arrays.toString(values).trim(); LOGGER.warn(valuesStr); I'm trying to look at a request parameter value using the code above. Why does Arrays.toString(values).trim(); bracket the parameter value so it looks like this: [Georgio] What's the best way to get the String here without the brackets? If I do this: String valuesStr = values[0].trim(); it seems there is a risk of losing subsequent values in the

lxml.etree fromsting() and tostring() are not returning the same data

99封情书 提交于 2019-12-06 01:15:40
I'm learning lxml (after using ElementTree) and I'm baffled why .fromstring and .tostring do not appear to be reversible. Here's my example: import lxml.etree as ET f = open('somefile.xml','r') data = f.read() tree_in = ET.fromstring(data) tree_out = ET.tostring(tree_in) f2 = open('samefile.xml','w') f2.write(tree_out) f2.close 'somefile.xml' was 132 KB. 'samefile.xml' - the output - was 113 KB, and it is missing the end of the file at some arbirtrary point. The closing tags of the overall tree and a few of the pieces of the final element are just gone. Is there something wrong with my code,

ToString() returns null?

僤鯓⒐⒋嵵緔 提交于 2019-12-05 19:01:29
I am trying to determine cause of 'Null reference exception' on published remote site. So, I can't debug it directly and can operate only with logs. So my question is: Is it possible, that .ToString() method of any of built-in .NET types returns null ? EDIT: I suspect DateTime.ToString(invariantCulture) method with badly constructed culture settings. I don't know of any types where it does. It's not impossible - it would certainly be easy to write your own type which behaved like that - but I doubt any of the framework types do. Do you have any particular types in mind? EDIT: DateTime.ToString

Since Int32 is a value type why does it inherit .ToString()?

别等时光非礼了梦想. 提交于 2019-12-05 16:57:45
问题 These are the docs about .ToString() that has prompted this question. They state: Because Object is the base class of all reference types in the .NET Framework, this behavior [.ToString()] is inherited by reference types that do not override the ToString method. Yet further on it goes to state: For example, the base types such as Char, Int32, and String provide ToString implementations However Int32 is a struct and hence must be a value type . So what's going on here? Does Int32 implement it

F#: shortest way to convert a string option to a string

為{幸葍}努か 提交于 2019-12-05 14:29:07
The objective is to convert a string option that comes out of some nicely typed computation to a plain string that can then be passed to the UI/ printf /URL/other things that just want a string and know nothing of option types. None should just become the empty string. The obvious way is to do a match or an if on the input: input |> fun s -> fun s -> match s with | Some v -> v | _ -> "" or input |> fun s -> if s.IsSome then s.Value else "" but while still being one-liners, these still take up quite a lot of line space. I was hoping to find the shortest possible method for doing this. You can

C# XMLElement.OuterXML in a single line rather than format

做~自己de王妃 提交于 2019-12-05 12:38:12
I am trying to log some XML responses from a WCF Service using log4net. I want the output of the XML file to the log to be in properly formed XML. The request comes in as an XMLElement. Example: The request comes in as this: <?xml version="1.0" encoding="utf-8"?> <ApplicationEvent xmlns="http://courts.wa.gov/INH_TV/ApplicationEvent.xsd"> <Severity xmlns="">Information</Severity> <Application xmlns="">Application1</Application> <Category xmlns="">Timings</Category> <EventID xmlns="">1000</EventID> <DateTime xmlns="">2012-09-02T12:05:15.234Z</DateTime> <MachineName xmlns="">Server1</MachineName>

javascript-类型转换

馋奶兔 提交于 2019-12-05 11:45:40
类型转换 JavaScript会根据需要自行转换类型 值 字符串 数字 布尔值 对象 undefined "undefined" NaN false throw TypeError null "null" 0 false throw TypeError true "true" 1 new Boolean(true) false "false" 0 new Boolean(false) ""(空串) 0 false new String("") "2.3" 2.3 true new String("2.3") "test" NaN true new String("test") 0 "0" false new Number(0) -0 "0" false new Number(-0) NaN "NaN" false new Number(NaN) Infinity "Infinity" true new Number(Infinity) -Infinity "-Infinity" true new Number(-Infinity) 1 "1" true new Number(1) {} 见下方对象转化为原始值 见下方对象转化为原始值 true [] "" 0 true [1] "1" 1 true ['a'] 使用join()方法 NaN true function(){} NaN

Javascript-基本类型

不想你离开。 提交于 2019-12-05 11:42:23
数字 JavaScript不区分整数和浮点数,所有数字都用浮点数表示。 能够表示最大值是 -2 53 ~ 2 53 ,包含边界。超过范围的数无法保证低位数字的精度。 JavaScript能直接识别十进制的整型直接量和十六进制值(以 0x 或 0X 为前缀,由 0~9 和 a(A)~f(F) 构成,如: 0xff ,即 15 * 16 + 15 = 255 )。ECMAScript标准不支持八进制直接量。 浮点数直接量表达式: [digits][.digits][(E|e)(+|-)digits] ,如: 3.12E12 => 3.12 12 , 浮点数值的最高精度是 17 位小数 算数运算符:加 + 、减 - 、乘 * 、除 / 、取余 % JavaScript中算数运算在溢出、下溢(无限接近于零并比JavaScript能表示的最小值还小的数,JavaScript将会返回0)或被零整除时不会报错(返回正(或负)无穷,例外:零除以零,结果是一个非数字,用 NaN 表示)。溢出时,结果为无穷大( Infinity )值;下溢结果为负无穷( -Infinity )。基于无穷大的加减乘除运算结果还是无穷大(保留正负号)。 NaN 表示非数字值,它和任何值都不相等,包括自身。判断一个值是否为NaN: x != x ,当切只当x为 NaN 时,表达式为true。 函数 isNaN()