[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 694
  • Last Modified:

Format String in ASP.net C#

I have a string strnames which contains following

Tony, Ray, Arnold, Wood, Ken, Jeff: Scheduled for workshop

What I want is to hide all the names after the 3rd occurrence of comma but then display the rest of the text after the colon

So the text will be like this (three dots showing there are more people, that I want to create a hyperlink to show entire string if someone clicks on the three dots just like facebook comments they have view more button)

Tony, Ray, Arnold ... : Scheduled for workshop
0
Guru Ji
Asked:
Guru Ji
  • 2
  • 2
1 Solution
 
binaryevoCommented:
You could use something like this to split up the strings and take the first 3 then cocantenate the results you want together:

string s = "tony, ray, arnold, wood, ken, jeff: scheudle";
 string[] myArray = s.Split(',');
var names = myArray.Take(3);

Open in new window


Hope this helps and if you need more guidance let me know.
0
 
Guru JiAuthor Commented:
The names var does not return string like
tony, ray, arnold

How do I display only first three comma delimited values with comma in between them.
0
 
binaryevoCommented:
Right the var is of type IEnumerable because we are using the linq .take().  If you want the string representation of this just loop through it and compile the string ( simplest way ).  So the whole bit of code would look like so:
string s = "tony, ray, arnold, wood, ken, jeff: scheudle";
string[] myArray = s.Spit(',');
var names = myArray.Take(3);
string myString = "";
foreach(var name in names){
        myString += name + ",";
}

//trim the last comma
var newString = myString.Remove(myString.Length - 1);

Open in new window

0
 
Guru JiAuthor Commented:
Hi binaryyevo,

I got it working, what I did I created two labels
label1.text = "tony, ray, arnold ... more : scheudle";
label2.text ="tony, ray, arnold, wood, ken, jeff: scheudle";
label2 is hidden by default.

How can I create that "... more" in label1 to a link which will let me hide label1 and show label2 without refreshing page.

My whole point of doing this is to hide usernames if showing more than 3 people
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now