string-concatenation

How to keep the spaces at the end and/or at the beginning of a String?

老子叫甜甜 提交于 2019-11-26 16:57:39
I have to concatenate these two strings from my resource/value files: <string name="Toast_Memory_GameWon_part1">you found ALL PAIRS ! on </string> <string name="Toast_Memory_GameWon_part2"> flips !</string> I do it this way : String message_all_pairs_found = getString(R.string.Toast_Memory_GameWon_part1)+total_flips+getString(R.string.Toast_Memory_GameWon_part2); Toast.makeText(this, message_all_pairs_found, 1000).show(); But the spaces at the end of the first string and at the beginning of the second string have disappeared (when the Toast is shown) ... What should I do ? I guess the answer

Javascript console.log(object) vs. concatenating string

廉价感情. 提交于 2019-11-26 16:31:24
问题 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' } ? 回答1: The + x coerces the object x into a string, which is just [object Object] : http://jsfiddle.net/Ze32g/ The pretty printing

Why to use StringBuffer in Java instead of the string concatenation operator

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 16:01:47
Someone told me it's more efficient to use StringBuffer to concatenate strings in Java than to use the + operator for String s. What happens under the hood when you do that? What does StringBuffer do differently? It's better to use StringBuilder (it's an unsynchronized version; when do you build strings in parallel?) these days, in almost every case, but here's what happens: When you use + with two strings, it compiles code like this: String third = first + second; To something like this: StringBuilder builder = new StringBuilder( first ); builder.append( second ); third = builder.toString();

How to efficiently concatenate strings in Go?

我只是一个虾纸丫 提交于 2019-11-26 15:34:21
In Go, a string is a primitive type, which means it is read-only, and every manipulation of it will create a new string. So if I want to concatenate strings many times without knowing the length of the resulting string, what's the best way to do it? The naive way would be: s := "" for i := 0; i < 1000; i++ { s += getShortStringFromSomewhere() } return s but that does not seem very efficient. marketer Note added in 2018 From Go 1.10 there is a strings.Builder type, please take a look at this answer for more detail . Pre-201x answer The best way is to use the bytes package. It has a Buffer type

Concatenating strings doesn&#39;t work as expected [closed]

我怕爱的太早我们不能终老 提交于 2019-11-26 15:27:04
问题 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

How do I concatenate strings?

。_饼干妹妹 提交于 2019-11-26 15:12:16
How do I concatenate the following combinations of types: str and str String and str String and String When you concatenate strings, you need to allocate memory to store the result. The easiest to start with is String and &str : fn main() { let mut owned_string: String = "hello ".to_owned(); let borrowed_string: &str = "world"; owned_string.push_str(borrowed_string); println!("{}", owned_string); } Here, we have an owned string that we can mutate. This is efficient as it potentially allows us to reuse the memory allocation. There's a similar case for String and String , as &String can be

String builder vs string concatenation [duplicate]

孤街醉人 提交于 2019-11-26 14:19:29
问题 This question already has answers here : StringBuilder vs String concatenation in toString() in Java (18 answers) Closed 2 years ago . 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. 回答1: In your particular example, none because

How to set the id attribute of a HTML element dynamically with angularjs (1.x)?

烈酒焚心 提交于 2019-11-26 14:06:17
Provided an HTML element of type div , how to set the value of its id attribute, which is the concatenation of a scope variable and a string ? ngAttr directive can totally be of help here, as introduced in the official documentation https://docs.angularjs.org/guide/interpolation#-ngattr-for-binding-to-arbitrary-attributes For instance, to set the id attribute value of a div element, so that it contains an index, a view fragment might contain <div ng-attr-id="{{ 'object-' + myScopeObject.index }}"></div> which would get interpolated to <div id="object-1"></div> This thing worked for me pretty

String concatenation without &#39;+&#39; operator

自作多情 提交于 2019-11-26 13:22:35
I was playing with python and I realized we don't need to use '+' operator to concatenate strings unless it is used with values. For example: string1 = 'Hello' 'World' #1 works fine string2 = 'Hello' + 'World' #2 also works fine string3 = 'Hello' string4 = 'World' string5 = string3 string4 #3 causes syntax error string6 = string3 + string4 #4 works fine Now I have two questions: Why statement 3 does not work while statement 1 does? Is there any technical difference such as calculation speed etc. between statement 1 and 2? From the docs : Multiple adjacent string literals (delimited by

Making a string concatenation operator in R

隐身守侯 提交于 2019-11-26 12:21:55
I was wondering how one might go about writing a string concatenation operator in R, something like || in SAS, + in Java/C# or & in Visual Basic. The easiest way would be to create a special operator using %, like `%+%` <- function(a, b) paste(a, b, sep="") but this leads to lots of ugly % 's in the code. I noticed that + is defined in the Ops group, and you can write S4 methods for that group, so perhaps something like that would be the way to go. However, I have no experience with S4 language features at all. How would I modify the above function to use S4? statsmaths As others have