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
Solved

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

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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…

809 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