String concatenation in c# and php

String concatenation in c# and php

String concatenation in c# and php

C#

Using + operator

Console.WriteLine("Hello" + " " + "String " + "!");

String Interpolation

string author = "Mahesh Chand";    
string book = "C# Programming";  
string bookAuthor = $"{author} is the author of {book}.";

String.Concatenate() method

string fName = "Mahesh";    
string lName = "Chand";    
string Name = string.Concat(fName, lName); 

string[] authors = { "Mahesh Chand ", "Chris Love ", "Dave McCarter ", "Praveen Kumar "};      
string arrayStr = string.Concat(authors); 

String.Join() method

int[] intArray = { 1, 3, 5, 7, 9 };
String seperator = ", ";
string result = "Int, ";
result += String.Join(seperator, intArray);

// Using String.Join(String, String[], int int)    
// Let's concatenate first two strings of the array    
String[] arr2 = { "Mahesh Chand ", "Chris Love ", "Dave McCarter ", "Praveen Kumar " };    
String seperator2 = ", ";    
string result2 = "First Author, ";    
result2 += String.Join(seperator2, arr2, 1, 2);    
Console.WriteLine($"Result: {result2}");

String.Format() method

string date = String.Format("Today's date is {0}", DateTime.Now);   

StringBuilder.Append() method

builder.Append(", ");

PHP

Using . operator

$b = "Hello " . "World!";   // slow

String Interpolation

$a = '3'; echo "qwe{$a}rty"; // double quote echo "Result: " . ($a + 3); // result 6 
"{$str1}{$str2}{$str3}"; // one concat = fast   $str1. $str2. $str3;    // two concats = slow 
//Use double quotes to concat more than two strings instead of multiple '.' operators.  PHP is forced to re-concatenate with every '.' operator.
$logMessage = "A {$user->type} with e-mailaddress {$user->email} has performed {$action} on {$subject}."

Using printf()

$logMessage = sprintf('A %s with email %s has performed %s on %s.', $user->type, $user->email, $action, $subject);

 

 521 total views

Author: Albert

Leave a Reply