Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# - String Parse after so many characters and then "..more"

Posted on 2012-04-10
4
Medium Priority
?
210 Views
Last Modified: 2012-06-27
Hello all,

I have a datatable with rows of course and I am just currently adding each column value to a string variable as below:

  foreach (DataRow row in dt.Rows)
                    {
                        strUniqueValues += row["MyColumn"].ToString() + ",";
                    }

What I was doing was if there was <= 3 rows then I just appended to the string otherwise in an else statement I was just doing a for loop 0-2 then I have a linkbutton that says "and 12 others" for example.

What I want to do is somehow have like say 300 characters max then cutoff and say "and 12 others" for example.  But I need it to be a clean cutoff where I show each whole string value then get a count of the remaining for the "and + cntremain + others".

Anyone know how I can do this?
0
Comment
Question by:sbornstein2
  • 2
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 1600 total points
ID: 37829912
Please try:
var msgTemplate = " and {0} others";
var comma = ",";
var sb = new StringBuilder(300);
var left = dt.Rows.Count;

foreach (DataRow row in dt.Rows)
{
    var str = row["MyColumn"].ToString();
    var msg = String.Format(msgTemplate, left);
    var extra = 1 + ((left > 1) ? msg.Length : comma.Length);
    
    if (str.Length + extra + sb.Length > sb.Capacity)
    {
        sb.Append(msg);
        
        break;
    }
    
    if (sb.Length > 0)
    {
        sb.Append(comma);
    }
    
    sb.Append(str);
    
    left--;
}

strUniqueValues = sb.ToString();

Open in new window

0
 

Author Closing Comment

by:sbornstein2
ID: 37832706
This was perfect, thank you very much for your time and help.
0
 

Author Comment

by:sbornstein2
ID: 37837808
wdosanjos sorry one more question.  I think I now need the same thing but not to cut off if the whole column text exceeds the character count.  How can I adjust this so lets say I still want 300 characters and then to have the "and {0} others" but have the last column text be something like "mytex..." just add dots to the last one.  Hope this makes sense.  So I want to still have 300 characters total but not cutoff the last column shorter if the whole column text won't make it.

My original post I wanted the opposite and only the whole text.

Thanks for any help again.
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 37839395
Add the following lines just before sb.Append(msg):
		int i = sb.Capacity - sb.Length - msg.Length - 4;

		if (i > 0)
		{
			sb.Append("," + str.Substring(0, i) + "...");
		}

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

824 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