?
Solved

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

Posted on 2006-11-30
3
Medium Priority
?
247 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

777 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