Solved

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

Posted on 2006-11-30
3
232 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
[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
3 Comments
 
LVL 30

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 30

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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

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…
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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