string-concatenation

What are the differences between concatenating strings with cat() and paste()?

牧云@^-^@ 提交于 2019-11-27 14:45:27
问题 What are the differences between concatenating strings with cat and paste ? In particular, I have the following questions. Why does R not use the double quote ( " ) when it prints the results of calling cat (but it uses quotes when using paste )? > cat("test") test > paste("test") [1] "test" Why do the functions length and mode , which are functions available for almost all objects in R, not "work" on cat ? > length(cat("test")) test[1] 0 > mode(cat("test")) test[1] "NULL" Why do C-style

Javascript console.log(object) vs. concatenating string

孤者浪人 提交于 2019-11-27 13:53:12
I'm running this in node.js: > x = { 'foo' : 'bar' } { foo: 'bar' } > console.log(x) { foo: 'bar' } undefined > console.log("hmm: " + x) hmm: [object Object] undefined What I don't understand is why console.log(x) "pretty-prints" the object, whereas string concatenation "ugly-prints" it. And more importantly, what's the best way to make it print hmm: { foo: 'bar' } ? The + x coerces the object x into a string, which is just [object Object] : http://jsfiddle.net/Ze32g/ The pretty printing is a very nice and probably very complex underlying code that someone implemented as part of the console

Concatenating strings doesn't work as expected [closed]

时光毁灭记忆、已成空白 提交于 2019-11-27 11:10:17
I know it is a common issue, but looking for references and other material I don't find a clear answer to this question. Consider the following code: #include <string> // ... // in a method std::string a = "Hello "; std::string b = "World"; std::string c = a + b; The compiler tells me it cannot find an overloaded operator for char[dim] . Does it mean that in the string there is not a + operator? But in several examples there is a situation like this one. If this is not the correct way to concat more strings, what is the best way? Your code, as written, works. You’re probably trying to achieve

const char* concatenation

会有一股神秘感。 提交于 2019-11-27 10:46:06
I need to concatenate two const chars like these: const char *one = "Hello "; const char *two = "World"; How might I go about doing that? I am passed these char* s from a third-party library with a C interface so I can't simply use std::string instead. In your example one and two are char pointers, pointing to char constants. You cannot change the char constants pointed to by these pointers. So anything like: strcat(one,two); // append string two to string one. will not work. Instead you should have a separate variable(char array) to hold the result. Something like this: char result[100]; //

String builder vs string concatenation [duplicate]

荒凉一梦 提交于 2019-11-27 08:50:13
This question already has an answer here: StringBuilder vs String concatenation in toString() in Java 19 answers What is the benefit and trade-off of using a string builder over pure string concatenation? new StringBuilder(32).append(str1) .append(" test: ") .append(val) .append(" is changed") .toString(); vs say str1 + " test: " + val + " is changed". str1 is a random 10 character string. str2 is a random 8 character string. In your particular example, none because the compiler internally uses StringBuilder s to do String concatenation. If the concatenation occurred in a loop, however, the

Concatenation operator (+) vs. concat() [duplicate]

删除回忆录丶 提交于 2019-11-27 08:50:09
This question already has an answer here: String concatenation: concat() vs “+” operator 11 answers For string concatenation we can use either the concat() or concat operator (+) . I have tried the following performance test and found concat() is faster and a memory efficient way for string concatenation. String concatenation comparison for 100,000 times : String str = null; //------------Using Concatenation operator------------- long time1 = System.currentTimeMillis(); long freeMemory1 = Runtime.getRuntime().freeMemory(); for(int i=0; i<100000; i++){ str = "Hi"; str = str+" Bye"; } long time2

Assumed string length input into a Fortran function

為{幸葍}努か 提交于 2019-11-27 08:23:42
问题 I am writing the following simple routine: program scratch character*4 :: word word = 'hell' print *, concat(word) end program scratch function concat(x) character*(*) x concat = x // 'plus stuff' end function concat The program should be taking the string 'hell' and concatenating to it the string 'plus stuff'. I would like the function to be able to take in any length string (I am planning to use the word 'heaven' as well) and concatenate to it the string 'plus stuff'. Currently, when I run

Python: Fast and efficient way of writing large text file

南楼画角 提交于 2019-11-27 07:51:24
问题 I have a speed/efficiency related question about python: I need to write a large number of very large R dataframe-ish files, about 0.5-2 GB sizes. This is basically a large tab-separated table, where each line can contain floats, integers and strings. Normally, I would just put all my data in numpy dataframe and use np.savetxt to save it, but since there are different data types it can't really be put into one array. Therefore I have resorted to simply assembling the lines as strings manually

Python Pandas concatenate a Series of strings into one string

↘锁芯ラ 提交于 2019-11-27 07:44:01
问题 In python pandas, there is a Series/dataframe column of str values to combine into one long string: df = pd.DataFrame({'text' : pd.Series(['Hello', 'world', '!'], index=['a', 'b', 'c'])}) Goal: 'Hello world !' Thus far methods such as df['text'].apply(lambda x: ' '.join(x)) are only returning the Series. What is the best way to get to the goal concatenated string? 回答1: You can join a string on the series directly: In [3]: ' '.join(df['text']) Out[3]: 'Hello world !' 回答2: Apart from join , you

R Dataframe: aggregating strings within column, across rows, by group

蹲街弑〆低调 提交于 2019-11-27 06:24:17
问题 I have what seems like a very inefficient solution to a peculiar problem. I have text data which, for various reasons, is broken across rows of a dataframe at random intervals. However, certain subsets of are known to belong together based on unique combinations of other variables in the dataframe. See, for example, a MWE demonstrating the structure and my initial solution: # Data df <- read.table(text="page passage person index text 1 123 A 1 hello 1 123 A 2 my 1 123 A 3 name 1 123 A 4 is 1