Format String in C#

Posted on 2012-08-21
Last Modified: 2012-08-23
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
Question by:Guru Ji
    LVL 14

    Expert Comment

    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.
    LVL 11

    Author Comment

    by:Guru Ji
    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.
    LVL 14

    Accepted Solution

    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

    LVL 11

    Author Comment

    by:Guru Ji
    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

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now