2017-05-02 15 views
5

Potrzebuję połączyć 3 wartości sesji Sesja ["Imię"], Sesja ["Drugie imię"], Sesja ["Nazwisko"] ze spacjami pomiędzy.Jak łączyć wartości sesji z spacją

Próbowałem następujące:

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

ale pojawia się wynik: firstnamemiddlenamelastname

+0

Ponieważ nie używasz spacji. Zamienić " ". Albo lepiej użyj string.format – Andrei

+0

użyj '" "' zamiast '" "' – Matthiee

Odpowiedz

0

Replace "" z "".

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

inny sposób:

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

Mam nadzieję, że to pomoże!

+0

Próbowałem tego .. ale zrób pracę –

+0

@AnitaMathew wypróbuj inną drogę zamiast w ten sposób. –

3

Nie łączysz spacji, ale puste struny.

var empty = "" 
var space = " " 

Więc trzeba zmienić swój przykład:

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

istnieją inne sposoby łączenia ciągów w języku C#.

pomocą operatora +:

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

Stosując C 6 interpolowanych ciągi funkcji:

Stosując string.Join:

Labelname.Text = string.Join(" ", new []{ this.Session["First Name"], this.Session["Middle Name"], this.Session["Last Name"]}); 
0

Prostym rozwiązaniem jest użycie String.Format

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

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

można wypróbować z poniżej metody.
Labelname.Text = this.Session ["First Name"] ToString() + "" + this.Session ["Middle Name"] ToString() + "" + this.Session ["Last Name"]. ToString();

Powiązane problemy