[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ASP.Net/C# - string split

Posted on 2009-04-30
12
Medium Priority
?
990 Views
Last Modified: 2012-05-06
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?
0
Comment
Question by:sbornstein2
  • 4
  • 4
  • 2
  • +1
12 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24270206
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
 
LVL 15

Expert Comment

by:spprivate
ID: 24270381
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
 
LVL 6

Expert Comment

by:MrJesse34
ID: 24271255
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:spprivate
ID: 24271623
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
 
LVL 6

Expert Comment

by:MrJesse34
ID: 24272299

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
 

Author Comment

by:sbornstein2
ID: 24272398
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
 

Author Comment

by:sbornstein2
ID: 24272409
ok fixed the bunching together issue that is fine now.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24272413
You should use Environment.NewLine as I demonstrated in my post.
0
 

Author Comment

by:sbornstein2
ID: 24272433
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
 
LVL 6

Expert Comment

by:MrJesse34
ID: 24272460
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
 
LVL 6

Accepted Solution

by:
MrJesse34 earned 1400 total points
ID: 24272481
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
 

Author Comment

by:sbornstein2
ID: 24272530
perfect awesome.  Thanks Jesse.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month17 days, 20 hours left to enroll

831 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