Solved

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

Posted on 2006-11-30
3
222 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Greetings C# program 17 107
Reference issues in Visual Studio 3 30
InputLanguage 1 27
Please explain the parts of these 2 LINQ expressions 3 33
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

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