Solved

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

Posted on 2012-04-10
4
177 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 400 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

707 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

12 Experts available now in Live!

Get 1:1 Help Now