String Concatenation Without '+' Operator

String concatenation without '+' operator

From the docs:

Multiple adjacent string literals (delimited by whitespace), possibly using different quoting conventions, are allowed, and their meaning is the same as their concatenation. Thus, "hello" 'world' is equivalent to "helloworld".


Statement 3 doesn't work because:

The ‘+’ operator must be used to concatenate string expressions at run time.

Notice that the title of the subheader in the docs is "string literal concatenation" too. This only works for string literals, not other objects.


There's probably no difference. If there is, it's probably extremely tiny and nothing that anyone should worry about.


Also, understand that there can be dangers to this:

>>> def foo(bar, baz=None):
... return bar
...
>>> foo("bob"
... "bill")
'bobbill'

This is a perfect example of where Errors should never pass silently. What if I wanted "bill" to be the argument baz? I have forgotton a comma, but no error is raised. Instead, concatenation has taken place.

Python string concatenation of multiple strings separated without comma

See https://docs.python.org/3.8/reference/lexical_analysis.html#string-literal-concatenation.

Multiple adjacent string or bytes literals (delimited by whitespace), possibly using different quoting conventions, are allowed, and their meaning is the same as their concatenation.

Thus, "hello" 'world' is equivalent to "helloworld". This feature can be used to reduce the number of backslashes needed, to split long strings conveniently across long lines, or even to add comments to parts of strings

String concatenation without altering original values - C

I ended up changing putEndpoint to a constant and created a buffer array and that I then copied putEndpoint into. This array then resets after each request.

const char putEndpoint[] = "PUT /api/v1/products/";
char http[] = " HTTP/1.1";
char productID[idLen];

char putRequestBuffer[100];
strcpy(putRequestBuffer, putEndpoint);
strcat(putRequestBuffer, productID);

char *putRequestEndpoint = strcat(putRequestBuffer,http);

Concatenate strings using ## operators in C

To use the call to puts() in this way, the macro catstr() should be constructed to do two things:

  • stringify and concatenate lmao to the string "catting these strings\t"
  • stringify and concatenate elephant to lmao.

You can accomplish this by changing your existing macro definition from:

#define catstr(x, y) x##y

To:

#define catstr(x, y) #x#y

This essentially result in:

"catting these strings\t"#lmao#elephant

Or:

"catting these strings   lmaoelephant"  

Making it a single null terminated string, and suitable as an argument to puts():

puts("catting these strings\t" catstr(lmao, elephant));

PHP string concat without the dot operator

In PHP, double quote (") delimited strings will evaluate variables in them.

$foo = 42;
echo "The answer for everything is $foo"; // The answer for everything is 42

This specific example is very bad because you shouldn't include variables directly in an SQL query, and shouldn't use mysql_query in new code.

See more:

  • Why shouldn't I use mysql_* functions in PHP?
  • How can I prevent SQL injection in PHP?

String concatenation: concat() vs + operator

No, not quite.

Firstly, there's a slight difference in semantics. If a is null, then a.concat(b) throws a NullPointerException but a+=b will treat the original value of a as if it were null. Furthermore, the concat() method only accepts String values while the + operator will silently convert the argument to a String (using the toString() method for objects). So the concat() method is more strict in what it accepts.

To look under the hood, write a simple class with a += b;

public class Concat {
String cat(String a, String b) {
a += b;
return a;
}
}

Now disassemble with javap -c (included in the Sun JDK). You should see a listing including:

java.lang.String cat(java.lang.String, java.lang.String);
Code:
0: new #2; //class java/lang/StringBuilder
3: dup
4: invokespecial #3; //Method java/lang/StringBuilder."<init>":()V
7: aload_1
8: invokevirtual #4; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
11: aload_2
12: invokevirtual #4; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
15: invokevirtual #5; //Method java/lang/StringBuilder.toString:()Ljava/lang/ String;
18: astore_1
19: aload_1
20: areturn

So, a += b is the equivalent of

a = new StringBuilder()
.append(a)
.append(b)
.toString();

The concat method should be faster. However, with more strings the StringBuilder method wins, at least in terms of performance.

The source code of String and StringBuilder (and its package-private base class) is available in src.zip of the Sun JDK. You can see that you are building up a char array (resizing as necessary) and then throwing it away when you create the final String. In practice memory allocation is surprisingly fast.

Update: As Pawel Adamski notes, performance has changed in more recent HotSpot. javac still produces exactly the same code, but the bytecode compiler cheats. Simple testing entirely fails because the entire body of code is thrown away. Summing System.identityHashCode (not String.hashCode) shows the StringBuffer code has a slight advantage. Subject to change when the next update is released, or if you use a different JVM. From @lukaseder, a list of HotSpot JVM intrinsics.

Where is the new Object of String created when we concat using + operator

First of all String s = new String("abs"); It will create two objects, one object in the pool area and another one in the non-pool area because you are using new and as well as a string literal as a parameter.

String str1 = "Hello";
String str2 = "World";
String str3 = new String("HelloWorld");
String str4 = str1 + str2;

Till now you have five String objects, four in String Constant Pool and one in Heap. So your str4 is a new object altogether inside the String Pool,
Please check the below code also,

 String str5="HelloWorld"; //This line will create one more String Constant Pool object because we are using the variable name as str5.
String str6="HelloWorld";////This line will not create any object, this will refer the same object str5.

For test

System.out.println(str3==str4); //false
System.out.println(str4==str5);//false
System.out.println(str5==str6);//true


Related Topics



Leave a reply



Submit