Concatenating strings doesn't work as expected
Your code, as written, works. You’re probably trying to achieve something unrelated, but similar:
std::string c = "hello" + "world";
This doesn’t work because for C++ this seems like you’re trying to add two char
pointers. Instead, you need to convert at least one of the char*
literals to a std::string
. Either you can do what you’ve already posted in the question (as I said, this code will work) or you do the following:
std::string c = std::string("hello") + "world";
Why doesn't my Java string concatenation work?
String
is immutable and concat() will return a new String
(check the linked doc), which you're not collecting.
You should make use of a StringBuilder to build a string efficiently, and then call toString()
on that once you're complete to get he resultant String.
e.g.
StringBuilder sb = new StringBuilder();
while (....) {
sb.append("more string data");
}
String str = sb.toString();
You can append Strings
e.g.
str = str + "more string data";
but it's not very efficient, due to the implementation of String
. A StringBuilder
is built in order to perform concatenation efficiently. You can tune a StringBuilder
via its initial capacity if you have an idea of the size of String
you're building.
You may see some sources refer to a StringBuffer
. That's very similar, except it's older and synchronises its methods by default. In a non-threaded environment that's wasteful and the general advice is to prefer StringBuilder
.
Python string concatenation not working as expected
No, it joins the elements of the list with the word 'Hello'
. For example, if you had ['A', 'B']
, it would produce 'AHelloB'
. Since there is only one element in your list, there is nothing to join, so it can just return the only element in there unchanged.
What you wanted is probably something like ' '.join(['Hello', 'world'])
.
Javascript - concat string does not work as expected
alert(user + "X") shows only h.reem
The ActiveX component is probably returning a null terminated string (I've seen this with Scripting.TypeLib & a couple of the AD objects for example) so concatenating it with another string fails. (You can verify this if 0 === user.charCodeAt(user.length - 1)
).
You will need remove the last character before using the string;
user = user.substr(0, user.length - 1);
Concatenating strings in iterating loop: += does not work as expected
I can assure you that there is no bug in SQL Server and that +=
works exactly as expected. I tried the following code:
DECLARE @#SomeTable TABLE (somecolumn varchar(8000));
INSERT @#SomeTable VALUES('a'), ('bbb'), ('ccccc');
DECLARE @SomeString varchar(8000) = 'init string',
@somecolumn varchar(8000);
WHILE EXISTS (SELECT * FROM @#SomeTable)
BEGIN
SELECT TOP 1 @somecolumn = somecolumn FROM @#SomeTable;
SET @SomeString += @somecolumn;
PRINT @SomeString; -- Works fine!!!
DELETE @#SomeTable Where somecolumn = @somecolumn;
END
And here are my results:
init stringa
init stringabbb
init stringabbbccccc
Since it's impossible to tell exactly what you're doing in your code (you've obfuscated the most important parts), maybe you could start from there? Surely either you have a NULL value in the table, or you're assigning incorrectly, or you're assigning to the wrong variable. Again, impossible to tell, because you've hidden the key parts of your code!
Also, since you don't seem to care about order, you can also do this without looping:
DECLARE @#SomeTable TABLE (somecolumn varchar(8000));
INSERT @#SomeTable VALUES('a'), ('bbb'), ('ccccc');
DECLARE @SomeString varchar(8000) = 'init string',
@somecolumn varchar(8000);
SELECT @SomeString += somecolumn FROM @#SomeTable;
PRINT @SomeString;
Result:
init stringabbbccccc
If you care about order, you can still do this without looping - use an XML trick to concatenate in that order, and then append it to the init string afterward:
DECLARE @#SomeTable TABLE (somecolumn varchar(8000));
INSERT @#SomeTable VALUES('a'), ('bbb'), ('ccccc');
DECLARE @SomeString varchar(8000) = 'init string',
@somecolumn varchar(8000) = '';
SELECT @somecolumn = (SELECT '' + somecolumn FROM @#SomeTable
ORDER BY somecolumn DESC
FOR XML PATH(''), TYPE).value(N'./text()[1]', N'varchar(max)');
PRINT @SomeString + @somecolumn;
Result:
init stringcccccbbba
On more modern versions (SQL Server 2017+), you can do this:
DECLARE @#SomeTable TABLE (somecolumn varchar(8000));
INSERT @#SomeTable VALUES('a'), ('bbb'), ('ccccc');
DECLARE @SomeString varchar(8000) = 'init string',
@somecolumn varchar(8000);
SELECT @somecolumn = STRING_AGG(somecolumn, '')
WITHIN GROUP (ORDER BY somecolumn DESC)
FROM @#SomeTable;
PRINT @SomeString + @somecolumn;
Why does string concat apply not work as expected?
The first argument of apply
is the context, which needs to be the string. You'd use
const arr = ["2","3"];
console.log("1".concat(...arr));
console.log(String.prototype.concat.apply("1", arr));
console.log("".concat.apply("1", arr));
In your particular case, I'd recommend to use rest/spread syntax:
function concat(x, ...args) {
return x.concat(...args);
}
or in ES5
function concat(x) {
var args = Array.prototype.slice.call(arguments, 1);
return x.concat.apply(x, args);
// ^
}
Doesn't return expected concatenate String
str.indexOf('z') == 1
returns false because indexOf
"returns the index within this string of the first occurrence of the specified character". In your case, str.indexOf('z')
is zero because zero is the first occurrence.
There is a version of this method which takes an additional argument that defines which index to start from, so you could use the following to check whether Z is the second character:
str.indexOf('z', 1) == 1
That said, a better approach to this problem is probably to use charAt
:
if (str.charAt(0) == 'o') //...
if (str.charAt(1) == 'z') //...
Related Topics
Do Negative Numbers Return False in C/C++
How Can It Be Useful to Overload the "Function Call" Operator
Maximum Number of Parameters in Function Declaration
C++ Stl: Array VS Vector: Raw Element Accessing Performance
Idiomatic Way to Declare C++ Immutable Classes
Dereferencing a Pointer When Passing by Reference
Is It Safe to Use the "This" Pointer in an Initialization List
How to Get the Icon, Mime Type, and Application Associated with a File in the Linux Desktop
Get a Single Line Representation for Multiple Close by Lines Clustered Together in Opencv
C++ Streams Confusion: Istreambuf_Iterator VS Istream_Iterator
Why Is Std::Vector::Operator[] 5 to 10 Times Faster Than Std::Vector::At()
Is Assignment Operator '=' Atomic
C++ Linking Error After Upgrading to MAC Os X 10.9/Xcode 5.0.1
How to Execute a Piece of Code Only Once