Solved

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

Posted on 2012-04-10
4
199 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

630 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