ASP.Net/C# - string split

I have a string that is long that I want to apply to a tooltip I have.  The issue is it expands way off the page.  The string looks like:

01111122,02552222,05844412,40052500,5005000,5000500

and goes on.  I want to somehow create the string so it has three account numbers and then a carriage return somehow.  How can I do this?
sbornstein2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
käµfm³d 👽Commented:
What about something like the following (where original holds the string to format):
string[] temp = original.Split(new char[] { ',' });
 
original = string.Empty;
for (int x = 0; x < temp.Length; x++)
{
    if (x % 3 == 0)
    {
        original = string.Concat(original, temp[x], Environment.NewLine);
    }
    else
    {
        original = string.Concat(original, temp[x], ',');
    }
}
 
original = original.Trim().TrimEnd(new char[] { ',' });

Open in new window

0
 
spprivateCommented:
The code below assumes you have only six account numbers so i use a function to get the 3rd occurance of ",".
If you need ,you need to develop a logic to find every third occurance extending my logic.
protected void Page_Load(object sender, EventArgs e)
 
    {
        String tooltext ="01111122,02552222,05844412,40052500,5005000,5000500";
        String c = ",";
        int ind = IndexOfNthOccurance(tooltext,c,3);
        tooltext = tooltext.Remove(ind, 1).Insert(ind, "\r\n");
        TextBox1.ToolTip = tooltext;
    }
    public int IndexOfNthOccurance(string s,string c , int occurance)
        {
            for (int i = 1, count = 0; i < s.Length; ++i)
            {
                if (s[i].ToString() == c)
                {
                    if (++count == occurance)
                    {
                        return i;
                    }
                }
            }
 
            return -1;
        }

Open in new window

0
 
MrJesse34Commented:
Use this:
string thisString = "01111122,02552222,05844412,40052500,5005000,5000500"
string [] stringColl = thisString.split(",");
foreach (string aString in stringColl)
{
  Textbox1.Tooltip += aString +  "\r\n";
}

Open in new window

0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
spprivateCommented:
Mr Jesse
You missed the point.He wanted the carriage return after every 3 account numbers.
I your case it will insert in all "," s.
0
 
MrJesse34Commented:

string thisString = "01111122,02552222,05844412,40052500,5005000,5000500"
string [] stringColl = thisString.split(",");
int count = 0;
foreach (string aString in stringColl)
{
  if (count == 3){
 Textbox1.Tooltip +=   "\r\n";
count = 0;
}
  Textbox1.Tooltip += aString;
count++;
}

Open in new window

0
 
sbornstein2Author Commented:
mjesse it seems to be bunching the values together before the r/n/  also I got an unterminated string error:

 protected void EDIJobData_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string listaccts = DataBinder.Eval(e.Row.DataItem, "listaccts").ToString();

                string[] stringColl = listaccts.Split(new char[] { ',' });

                int count = 0;

                listaccts = string.Empty;

                foreach (string aString in stringColl)
                {
                  if (count == 3){
                      listaccts += "\r\n";
                       count = 0;
                }
                listaccts += aString;
                count++;
                }

                e.Row.Attributes.Add("onmouseover", "ddrivetip('" + listaccts + "','#FFCC66', '400');");
                e.Row.Attributes.Add("onmouseout", "hideddrivetip()");
            }
        }
0
 
sbornstein2Author Commented:
ok fixed the bunching together issue that is fine now.
0
 
käµfm³d 👽Commented:
You should use Environment.NewLine as I demonstrated in my post.
0
 
sbornstein2Author Commented:
looks like its doing the following:

<tr onmouseover="ddrivetip(' 01403161 01552414 01701489
 01686080','#FFCC66', '400');" onmouseout="hideddrivetip()" style="color:#333333;background-color:#76B0F0;">
                                          <td>

then getting the javascript error:  Unterminated string constant.
0
 
MrJesse34Commented:
Ok I know what the problem is.  Once you add the text to the tooltip, you don;t need the javascript code.  As soon as you hover over the control, the text will display.  Its native functionality in the tooltip.
0
 
MrJesse34Commented:
get rid of this code here:

e.Row.Attributes.Add("onmouseover", "ddrivetip('" + listaccts + "','#FFCC66', '400');");
e.Row.Attributes.Add("onmouseout", "hideddrivetip()");

And add in the text for the tooltip like this:

e.Row.Tooltip =  listaccts;
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
sbornstein2Author Commented:
perfect awesome.  Thanks Jesse.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.