string-concatenation

How many Strings are created in memory?

落花浮王杯 提交于 2019-11-30 06:01:32
问题 Say I have this String expression String hi = "Tom" + "Brady" + "Goat" I know that the String pool "allows a runtime to save memory by preserving immutable strings in a pool" String Pool How many strings will be created in the string pool? My initial guess was 5 - "Tom" , "Brady" , "Goat" , "TomBrady" , "TomBradyGoat" , because of the order of operations of String concatenation (left to right?) or is it only the final result, "TomBradyGoat", that is stored in the String pool? 回答1: At runtime,

How should I allocate memory for c-string char array?

孤人 提交于 2019-11-30 05:47:31
问题 So in attempting to learn how to use C-Strings in C++, I'm running into issues with memory allocation. The idea here is that a new string is created of the format (s1 + sep + s2) The text I'm using provided the header, so I can't change that, but I'm running into issues trying to set the size of char str[]. I am getting an error saying that sLength is not constant, and therefore cannot be used to set the size of an array. I'm relatively new to C++ so this is a two part question. Is this

Is string concatenation in scala as costly as it is in Java?

帅比萌擦擦* 提交于 2019-11-30 05:35:40
In Java, it's a common best practice to do string concatenation with StringBuilder due to the poor performance of appending strings using the + operator. Is the same practice recommended for Scala or has the language improved on how java performs its string concatenation? Scala uses Java strings ( java.lang.String ), so its string concatenation is the same as Java's: the same thing is taking place in both. (The runtime is the same, after all.) There is a special StringBuilder class in Scala, that "provides an API compatible with java.lang.StringBuilder "; see http://www.scala-lang.org/api/2.7

C string concatenation of constants

冷暖自知 提交于 2019-11-30 00:29:21
问题 One of the answers to Why do you not use C for your web apps? contains the following: For the C crap example below: const char* foo = "foo"; const char* bar = "bar"; char* foobar = (char*)malloc(strlen(foo)+strlen(bar)+1); strcpy(foobar, foo); strcat(foobar, foo); Actually, constants CAN AND SHOULD be concatenated naturally in C: const char foo[] = "foo"; const char bar[] = "bar"; char foobar[] = foo bar; // look Ma, I did it without any operator! And using [] instead of * will even let you

Concatenation of Strings and characters

做~自己de王妃 提交于 2019-11-29 23:30:59
The following statements, String string = "string"; string = string +((char)65) + 5; System.out.println(string); Produce the output stringA5 . The following however, String string = "string"; string += ((char)65) + 5; System.out.println(string); Produce string70 . Where is the difference? Kai Sternad You see this behavior as a result of the combination of operator precedence and string conversion. JLS 15.18.1 states: If only one operand expression is of type String, then string conversion (§5.1.11) is performed on the other operand to produce a string at run time. Therefore the right hand

Can I use require(“path”).join to safely concatenate urls?

℡╲_俬逩灬. 提交于 2019-11-29 21:59:16
Is this safe to use require("path").join to concatenate URLs, for example: require("path").join("http://example.com", "ok"); //returns 'http://example.com/ok' require("path").join("http://example.com/", "ok"); //returns 'http://example.com/ok' If not, what way would you suggest for doing this without writing code full of ifs? Matthew Bakaitis No. path.join() will return incorrect values when used with URLs. It sounds like you want url.resolve . From the Node docs : url.resolve('/one/two/three', 'four') // '/one/two/four' url.resolve('http://example.com/', '/one') // 'http://example.com/one'

How do you append to an already existing string?

天大地大妈咪最大 提交于 2019-11-29 20:24:48
I want append to a string so that every time I loop over it will add say "test" to the string. Like in PHP you would do: $teststr = "test1\n" $teststr .= "test2\n" echo = "$teststr" echos: test1 test2 But I need to do this in a shell script William Pursell In classic sh, you have to do something like: s=test1 s="${s}test2" (there are lots of variations on that theme, like s="$s""test2" ) In bash, you can use +=: s=test1 s+=test2 $ string="test" $ string="${string}test2" $ echo $string testtest2 #!/bin/bash message="some text" message="$message add some more" echo $message some text add some

String concatenation with Groovy

梦想的初衷 提交于 2019-11-29 19:30:14
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 "+" expression to be a String, no matter if it's in the first place or in the last

Concatenating strings with `if` statements in JavaScript

怎甘沉沦 提交于 2019-11-29 16:03:43
问题 I'm attempting to set up a script to concatenate some variables inside a string if they exist , in order to place the appropriate metadata tags into a rendered HTML document. My concatenation code is: data = "<html>\n<head>\n" + "</head>\n<body>\n\n" + paras.join("\n\n") + "\n\n</body>\n</html>"; I'm trying to add if statements like the following into it (between the first and second items): if (typeof metadata_title !== "undefined") { "<title>" + metadata_title + "</title>\n" } if (typeof

concatenating string

戏子无情 提交于 2019-11-29 15:49:07
Is there a way in SQL sever that can write the output as follow: select events from mytable original output events -------- 123456 894531 985233 829292 920202 392939 299223 desired output '123456', '894531','985233','829292','920202','392939','299223' select '' + CustomerID + ',' from dbo.Customers customerid ALFKI, ANATR, ANTON, AROUT, BERGS, Would like to see the result as customerid 'ALFKI', 'ANATR', 'ANTON', 'AROUT', 'BERGS', so on... SELECT STUFF( (SELECT ', ' + events FROM dbo.mytable FOR XML PATH('') ), 1, 1, '') As concatenated_string If you want the values enclosed in single quotes