Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate session values with a space

I need to concatenate 3 session values Session["First Name"],Session["Middle Name"],Session["Last Name"] with spaces in between.

I tried the following :

     Labelname.Text = String.Concat(this.Session["First Name"],"", this.Session["Middle Name"],"", this.Session["Last Name"]);

but I get the result as : firstnamemiddlenamelastname

like image 610
Anita Mathew Avatar asked Oct 18 '22 13:10

Anita Mathew


1 Answers

You are not concatenating spaces, but empty strings.

var empty = ""
var space = " "

So, you need to change your example:

Labelname.Text = String.Concat(this.Session["First Name"]," ", this.Session["Middle Name"]," ", this.Session["Last Name"]);

There are other ways to concatenate strings in C#.

Using the + operator:

 Labelname.Text = this.Session["First Name"] + " " + this.Session["Middle Name"] + " " + this.Session["Last Name"];

Using the C# 6 interpolated strings feature:

 Labelname.Text = $"{this.Session["First Name"]} {this.Session["Middle Name"]} {this.Session["Last Name"]}";

Using string.Join:

Labelname.Text = string.Join(" ", new []{ this.Session["First Name"], this.Session["Middle Name"], this.Session["Last Name"]});
like image 89
DigiFriend Avatar answered Oct 27 '22 14:10

DigiFriend