Solved

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

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

821 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