Solved

Manipulate arrays, and placing the results to SQL - slight problem with the replace method

Posted on 2006-11-30
3
212 Views
Last Modified: 2010-04-16
I have the following code that Agarici helped me with:

string page = "[[null,'menu test','_self','sada',[null,'dasdas asdasd','_self','sada',[null,'dasdas aa','_self','sada'],[null,'dasdas','_self','sada',[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada']],[null,'dasdas','_self','sada']]],[null,'dasdas','_self','sada',[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada',[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada']]],[null,'dasdas','_self','sada']]";

            int itemId = 1;
            int parentId = 0;
            int crtPos = 0;

            string allEntries = page.Replace("\n", string.Empty);
            allEntries = allEntries.Replace("\r", string.Empty).Replace(" ", string.Empty).Replace("\t", string.Empty); // I THINK ITS WRONG HERE

            string[] values = allEntries.Split('[');
            string crtValue = string.Empty;
            string query = string.Empty;
            string initialCrtValue = string.Empty;
            string paveWithIdAndParentId = string.Empty;
            string pageWithIdAndParentId = "[";

            ArrayList parents = new ArrayList();

            for ( int i = 0 ; i < values.Length ; i++ )
            {
                crtValue = values[i];

                if ( crtValue.Length == 0 )
                {
                    continue;
                }
                initialCrtValue = "[" + crtValue;

                itemId ++;
                parentId = itemId;

                crtPos = crtValue.IndexOf(']');
                if ( crtPos == -1 )
                {
                    if ( parents.Count > 0 )
                        parentId = (int)parents[parents.Count - 1];
                    parents.Add(itemId);
                }
                else
                {
                    crtValue = crtValue.Remove(crtPos, 1);
                    crtPos = crtValue.IndexOf(']');
                    if ( parents.Count > 0 )
                    {
                        parentId = (int)parents[parents.Count - 1];
                    }
                    while ( crtPos != -1 )
                    {
                        if ( parents.Count > 0 )
                        {
                            parents.RemoveAt(parents.Count - 1);
                        }
                        crtValue = crtValue.Remove(crtPos, 1);
                        crtPos = crtValue.IndexOf(']');
                    }
                }

                //query = string.Format("INSERT INTO yourTableHere (menu_Icon,menu_Title,menu_URL,menu_Target ,menu_description, Id, ParentId) VALUES (%s%d,%d)", values, id, parentId);
                query = string.Format("({0}{1},{2})", crtValue, itemId, parentId);
                Console.WriteLine(query);
                // execute query string formated above.
                int firstComaPos = initialCrtValue.IndexOf(',');
                if ( firstComaPos == -1 ) continue;
                initialCrtValue = initialCrtValue.Insert(firstComaPos + 1, string.Format("{0},{1},", itemId, parentId));
                pageWithIdAndParentId += (initialCrtValue);

==============================================================================

strin being passed:

string page = "[[null,'menu test','_self','testing 123',[null,'mouse mat','_self','sada',[null,'dasdas aa','_self','sada'],[null,'dasdas','_self','sada',[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada']],[null,'dasdas','_self','sada']]],[null,'dasdas','_self','sada',[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada',[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada']]],[null,'dasdas','_self','sada']]";

The problem is with this line (I think)

allEntries = allEntries.Replace("\r", string.Empty).Replace(" ", string.Empty).Replace("\t", string.Empty); // I THINK ITS WRONG HERE

It strips all the space, so where I have 'menu test', or 'mouse mat', or 'testing 123' ... it strips the spaces

Can someone please help me in getting this slightly changed so that it works with sapce? The thing is I need the space, I cant have:

menutest or testing123, or mousemat. Acually where ever there are "legitimate" space, please dont change them.

Many thanks and kind regards
Mousemat24
0
Comment
Question by:mousemat24
  • 2
3 Comments
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 18049828
If you want to replace some spaces but want to preserve others you need to write your own procedure.

I believe the algorithm might be:
count single quoter marks. A space between two single quotas are "legitimate", yes?
if space follows odd count quota it should be preserved.
Procedure something as follows :

            string s = "hello 'bn hg'jh uy";
            int count = 0;
            StringBuilder sb = new StringBuilder(s.Length);
            foreach (char c in s)
            {
                if(c=='\'')
                {
                    count++;
                    sb.Append(c);
                }
                else if (c == ' ' && count % 2 == 0 || c == '\t' || c == '\r' || c == '\n')
                {
                    // do nothing
                }
                else
                {
                    sb.Append(c);
                }
               
            }
           
            Debug.WriteLine(sb.ToString());

Result printed:
hello'bn hg'jhuy - space between quotas is preserved.
The procedure might be quite slow, however.
0
 

Author Comment

by:mousemat24
ID: 18050085
Thanks anarki_jimbel
Thats will help me.

anarki_jimbel, how good is your knowledge in ASP.NET treeview. I've posted a question on a NavigateURL, no one has answered, and I really need this sorted out ASAP.

Thanks
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 18050771
I'm not really working with ASP unfortunately

What I got from MSDN:
When a node is in navigation mode, all selection events are disabled for that node.

And it seems you are working in navigation mode. Therefore node is not selected.

I'm not sure how to walk around the problem. However what if you try to change some properties of the clicked node, e.g. set it's background color to some different value?

Another solution might be to work in selection mode and do not set NavigateUrl property (leave it as ""). Put you url into value property and when it's clicked use value to navigate to url...

Sorry, probably not much help.. :)
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now