string-concatenation

Concatenating numbers as string in Javascript

跟風遠走 提交于 2020-01-30 06:38:26
问题 myCoolObject { a: 0 b: 12 c: 24 } I want to concatenate a , b and c so that they look like a unique string "a-b-c" (or "0-12-24" in the example). a , b and c will always represent numbers. Converting each one of them from int to string require a lot of code: I'd just use sprintf() if I were in PHP or C, but how can I do this in JS without using toString() for each parameter and writing too much code? Whole code: var pickedDate = this.getSelectedDay().year.toString() + "-" + this

Why does + work with Strings in Java?

一世执手 提交于 2020-01-28 09:25:11
问题 Java can't do operator overloading, but + works okay for String and Integer and some other classes. How is this possible? update: Why does this work? Integer i = 4; Integer p = 5; System.out.println(i*p); // prints 20 回答1: + is not an example of operator overloading. + is built into the language as a concatentation operator and an arithmetic-addition operator. What this means is that a person writing a program with Java cannot overload operators, but as far as the grammar of the Java language

How do you concatenate two strings? [duplicate]

雨燕双飞 提交于 2020-01-25 03:15:07
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: How do I concatenate multiple C++ strings on one line? How would I take: string test1 = "Hello "; string test2 = "World!"; and concatenate them to make one string? 回答1: How about string test3 = test1 + test2; Or maybe test1.append(test2); 回答2: You could do this: string test3 = test1 + test2; Or if you want to add more string literals, then : string test3 = test1 + test2 + " Bye bye World!"; //ok or, if you want

Using character instead of String for single-character values in StringBuffer append

无人久伴 提交于 2020-01-24 02:18:07
问题 I was going through the PMD rule AppendCharacterWithChar . It says Avoid concatenating characters as strings in StringBuffer.append. StringBuffer sb = new StringBuffer(); // Avoid this sb.append("a"); // use instead something like this StringBuffer sb = new StringBuffer(); sb.append('a'); Do I really need this PMD rule ? Is there much performance difference between the following two piece of code? String text = new StringBuffer().append("some string").append('c').toString(); String text = new

Joining two strings with a comma and space between them

安稳与你 提交于 2020-01-22 05:50:05
问题 I have been given the two strings "str1" and "str2" and I need to join them into a single string. The result should be something like this: "String1, String 2" . The "str1" and "str2" variables however do not have the ", " . So now for the question: How do I join these strings while having them separated by a comma and space? This is what I came up with when I saw the "task", this does not seperate them with ", " though, the result for this is “String2String1”. function test(str1, str2) { var

Merging a list of Strings using mkString vs foldRight

孤人 提交于 2020-01-20 04:02:31
问题 I am currently trying out things in Scala, trying to get accustomed to functional programming as well as leaning a new language again (it's been a while since last time). Now given a list of strings if I want to merge them into one long string (e.g. "scala", "is", "fun" => "scalaisfun" ) I figured one way to do it would be to do a foldRight and apply concatenation on the respective elements. Another way, admittedly much simpler, is to call mkString . I checked on github but couldn't really

Sets From a Single Table, Grouped By a Column

纵饮孤独 提交于 2020-01-16 14:37:22
问题 I have a table: +-------+-------+----------+ | GROUP | State | Priority | +-------+-------+----------+ | 1 | MI | 1 | | 1 | IA | 2 | | 1 | CA | 3 | | 1 | ND | 4 | | 1 | AZ | 5 | | 2 | IA | 2 | | 2 | NJ | 1 | | 2 | NH | 3 | And so on... How do I write a query that makes all the sets of the states by group, in priority order? Like so: +-------+--------------------+ | GROUP | SET | +-------+--------------------+ | 1 | MI | | 1 | MI, IA | | 1 | MI, IA, CA | | 1 | MI, IA, CA, ND | | 1 | MI, IA, CA

How to concatenate all results from table row?

喜夏-厌秋 提交于 2020-01-14 18:10:44
问题 Can not find a solution to do something like: SELECT CONCAT_WS(',', ( SELECT * FROM table WHERE id = 1 )) How can I do that in PostgreSQL? 回答1: Quick and dirty: SELECT t::text FROM tbl t WHERE id = 1; t is an alias for the table and not strictly needed. You can use the original table name as well. But if you have a column of the same name it takes precedence. So t represents the row type of the table, which is automatically coerced to text representation on output. I added an explicit cast to

How to concatenate all results from table row?

本小妞迷上赌 提交于 2020-01-14 18:10:30
问题 Can not find a solution to do something like: SELECT CONCAT_WS(',', ( SELECT * FROM table WHERE id = 1 )) How can I do that in PostgreSQL? 回答1: Quick and dirty: SELECT t::text FROM tbl t WHERE id = 1; t is an alias for the table and not strictly needed. You can use the original table name as well. But if you have a column of the same name it takes precedence. So t represents the row type of the table, which is automatically coerced to text representation on output. I added an explicit cast to

Why doesn't join() automatically convert its arguments to strings? When would you ever not want them to be strings?

旧街凉风 提交于 2020-01-14 07:32:08
问题 We have a list: myList = [1, "two"] And want to print it out, normally I would use something like: "{0} and {1}".format(*myList) But you could also do: " and ".join(myList) But unfortunately: >>> " and ".join(myList) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: sequence item 0: expected string, int found Why doesn't it just automatically convert the list it receives to strings? When would you ever not need it to convert them to strings? Is there some tiny