We help IT Professionals succeed at work.

Show details based on values

mousemat24
mousemat24 asked
on
Hi there

Wonder if you can help

I have these string, any of which can be blank:
private string GetContactDetails(User user)
{
  string contact = string.Format("Contact:\t{0}\r\n", user.FullName);
  string email = string.Format("Email:\t{0}\r\n", user.Email);
  string phone = string.Format("Phone:\t{0}\r\n", user.Phone);
}

what I want to to return a string based on the values above i.e.

if contact and email has a value

show:

Contact:  Rob
Email:    asdasd@hotmail.com


or if none of them have values, dont return any text


or if ONLY the phone has a value, show

Phone:    0283834757



etc


hope this makes sense?

thanks
Comment
Watch Question

Sr. Systems Engineer
Commented:
Something like:

string contact = user.FullName == "" ? string.empty : string.Format("Contact:\t{0}\r\n", user.FullName);
string email = user.Email == "" ? string.empty : string.Format("Email:\t{0}\r\n", user.Email);
string phone = user.Phone == "" ? string.empty : string.Format("Phone:\t{0}\r\n", user.Phone);



Gary ColtharpSr. Systems Engineer

Commented:
One small correction:

string contact = user.FullName == "" ? string.Empty : string.Format("Contact:\t{0}\r\n", user.FullName);
string email = user.Email == "" ? string.Empty : string.Format("Email:\t{0}\r\n", user.Email);
string phone = user.Phone == "" ? string.Empty : string.Format("Phone:\t{0}\r\n", user.Phone);
Commented:
@gcoltharp:

you forgot to add:

string result = contact + email + phone

Explore More ContentExplore courses, solutions, and other research materials related to this topic.