Solved

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

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 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