How to combine session values ​​with a space

I need to combine the 3 values ​​of the Session ["First Name"], Session ["Middle Name"], Session ["Last Name"] session with spaces between them.

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 like: firstnamemiddlenamelastname

+5
source share
5 answers

You do not combine spaces, but empty lines.

 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 interpolated string function C # 6:

  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"]}); 
+3
source

Replaced by" ".

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

Another way:

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

Hope this helps!

0
source

A simple solution is to use string.format

 string.Format("{0} {1} {2}", this.Session["First Name"], this.Session["Middle Name"], this.Session["Last Name"]); 
0
source

Using C # v6 +

 var firstName = this.Session["First Name"].ToString(); var middleName = this.Session["Middle Name"].ToString(); var lastName = this.Session["Last Name"].ToString(); Labelname.Text = $"{firstName} {middleName} {lastName}"; 
0
source

You can try using the method below.
Labelname.Text = this.Session ["First Name"]. ToString () + "+ this.Session [" Middle Name "]. ToString () +" + this.Session ["Last Name"] ToString ();

0
source

All Articles