www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 23

Thread: Looping through string variable!

  1. #1
    Join Date
    Oct 2005
    Posts
    252

    Looping through string variable!

    Hi all..
    I'm making a custom HTMLarea for myself, i'm using special tags i made to modify text formating, like the size and color,......., Just like this forums HTMLarea
    then i replace this tags with html tags "<font size=1>some text here</font>" using the Replace method for string, and it works pretty fine, but the problem is that it changes some text that i dont want change, like if i posted a code containge "[]" for looping for example.
    So what i want is to loop through the text variable and change each tag i want to change. How can i do that?!

    Thank you very much.

  2. #2
    Join Date
    Sep 2006
    Location
    Kentucky
    Posts
    56
    I had written this block of code a few weeks ago to strip out any html tags in the text. _body is the string variable I used to hold the text loaded from a database.

    Code:
    Dim _body as string = "<b>Test</b>. <u>This is </u> a test to remove formatting."
    Dim _lt As String = "<"
    Dim _gt As String = ">"
    Dim _value As Integer = _body.IndexOf(_lt)
    Dim _value2 As Integer = _body.IndexOf(_gt, _value) + 1
    
    _body = _body.Replace(_body.Substring(_value, _value2 - _value), "")
    You could modify this to replace the text between the tags instead of removing the tags like I did.

  3. #3
    Join Date
    Apr 2005
    Posts
    634
    Hi, another alternative is to make use the XMLDocument Class.

    The load method allow you the load a entire doc into class where the LoadXml method allows you to load just a xml string (you probably more interested in this method...)

    for example, if you have the following custom html:
    dim htmlString as String = "<font size='1'>some text here</font>"

    you can strip it with the following snippet of code:
    Code:
    'method stripe html tag and put inner string
        Public Function stripHTMLString(ByVal htmlString As String) As String
            Dim xmlString As System.Xml.XmlDocument = New             System.Xml.XmlDocument
            xmlString.LoadXml(htmlString)
            Return xmlString.InnerText
        End Function
    to use the function (supposed we created an object call HTMLDriver):
    Response.write(HTMLDriver.stripHTMLString(htmlString))


    for complex custom html tag, u can even use xpath to further customer and control the inner text as well.

    see if that helps...

  4. #4
    Join Date
    Oct 2005
    Posts
    252
    i'll show u example of what i tried to do to understand more what i want, but i think now we are getting so close
    Code:
    private string ModifyBody(string Body)
        {
            //Modify The Size
            char[] charBody = Body.ToCharArray();
            StringBuilder sb = new StringBuilder();
            string strModBody = "";
            string strBody = "";
    
            for (int i = 0; i < charBody.Length; i++)
            {
                sb.Append(charBody[i].ToString());
                //strBody = sb.ToString();
    
                if (sb.ToString().IndexOf("[size=") != -1 && sb.ToString().IndexOf("[/size]") != -1)
                {
                    sb.Replace("]", ">");
                    sb.Replace("[size=", "<font size=");
                    sb.Replace("[/size", "</font");
                }
            }
    
            Body = sb.ToString();//strModBody;
    
            return Body;
        }
    now this works pretty fine, but the problem is in this tags
    Code:
    "]", ">"
    that if there's other tags between the size tag it will change which i dont want to happen.
    so, i tried to use this but i dont know how to make it work
    Code:
     for (int i = 0; i < charBody.Length; i++)
            {
                sb.Append(charBody[i].ToString());
                strBody = sb.ToString();
    
                if (sb.ToString().IndexOf("[size=") != -1 && sb.ToString().IndexOf("[/size]") != -1)
                {
                    //i think this doesnt work fine in the loop
                    strModBody = strBody.Substring(strBody.IndexOf("[size="), (strBody.IndexOf("[/size]") + 7));
                    strModBody = strModBody.Replace("[size=", "<font size=");
                    strModBody = strModBody.Replace("[/size]", "</font>");
                    strModBody = strModBody.Replace("]", ">");
                }
            }
    Last edited by Cipher; 10-02-2006 at 06:31 PM.

  5. #5
    Join Date
    Apr 2005
    Posts
    634
    use of regular expression should be able to do the trick...

    suppose we have a string:
    Code:
    [font size='6'] aaa]aa [/font]
    we can set up a method:
    Code:
    //will return orginal parameter if string doesn't match exact systax:
    //[font size='(something)']  (something) [/font]
    private string stripFontTag(string fontString){
    //first grab the size attribute...
      string sizeValue = Regex.Match(fontString,"[font size='[0-9]']",RegexOptions.IgnoreCase).Value;
      sizeValue = sizeValue.Substring(0,1);
    //replace the first [
      fontString = Regex.Replace(fontString, "[" + Regex.Escape("[") + "]font size='" + sizeValue + "']" ,"<font size='" + sizeValue + "']",RegexOptions.IgnoreCase);
    //replace the first ]
    fontString = Regex.Replace(fontString, "<font size='" +   sizeValue + "'[" + Regex.Escape("]") + "]","<font size='" + sizeValue + "'>",RegexOptions.IgnoreCase);
    //replace the closing tag
      fontString = Regex.Replace(fontString, "[" + Regex.Escape("[") + "]/font[" + Regex.Escape("]") + "]" ,"</font>",RegexOptions.IgnoreCase);
    return fontString;
    }//end stripFontTag
    (suppose we created an object HTMLdriver) so just go ahead and call the method:

    response.write(HTMLdriver.stripFontTag(txtInput.Text);


    here's the reference for usage with regular expression...
    Last edited by sirpelidor; 10-03-2006 at 02:21 AM.

  6. #6
    Join Date
    Oct 2005
    Posts
    252
    I think Regular expressions is so boring, it contains alot of sybmols that will take alot of time to understand, and i think it will work better for validating data than replacing text if string variable. dnt you think?!
    isnt there better way to do so with normal substring and replacr methods!

  7. #7
    Join Date
    Apr 2005
    Posts
    634
    Quote Originally Posted by Cipher
    I think Regular expressions is so boring, it contains alot of sybmols that will take alot of time to understand, and i think it will work better for validating data than replacing text if string variable. dnt you think?!
    isnt there better way to do so with normal substring and replacr methods!

    I'm sure there are always alternative. However, I won't be the guy who can judge what is "better" and what is not, I tend to use what I feel is right.

    I think the reason I pick regular expression as a solution implementation is because I personally want to match just beyong the char (]) problem. I was more concern of invalid tags (i.e: [font size='1'] [font size='3'] [/font]). Using regular expression will allows me to watch out of invalid tag first before i ever try to phase them into html.

    I think you can combine methods from string class to accomplish what you wanted, if you use the "look ahead" approach. Meaning you have to prase the entire string into memory first, and then make mutiple trips (read entire string backward and forward) to make sure the logic (tag match, tag attribute match, etc) is in place.

    Good luck with you program.

  8. #8
    Join Date
    Oct 2005
    Posts
    252
    Well, thank you, i've been reading in regex for long and i think it will take me long time to do what i want, would you check this out?!
    1st, here's the original text
    Code:
    [size= 7]just new function[/size]
    [size= 5]all new body function[/size]
    [color= #A0522D]Color Text[/color]
    [size= 3]the one body function[/size]
    and here's what i'm trying to do
    Code:
    private string ModifyBody(string Body)
        {
            //Modify The Size
            char[] charBody = Body.ToCharArray();
            StringBuilder sb = new StringBuilder();
            string strBody = "";
            string modBody = "";
    
            for (int i = 0; i < charBody.Length; i++)
            {
                sb.Append(charBody[i].ToString());
                strBody = sb.ToString();
    
                if (strBody.IndexOf("[size=") != -1 && strBody.IndexOf("[/size]") != -1)
                {
                    // cut the tags and text in tags and put them in other string
                    modBody = strBody.Substring(strBody.IndexOf("[size="), (strBody.IndexOf("[/size]") + 7));
    
                    // modify the tags and replace them with html tags
                    modBody = modBody.Replace("[size=", "<font size=");
                    modBody = modBody.Replace("[/size", "</font");
                    modBody = modBody.Replace("]", ">");
                    
                    // place the new modified string in the sbvariable
                    sb = sb.Insert(strBody.IndexOf("[size="), modBody);
                    //sb = sb.Remove(strBody.IndexOf("[size="), (strBody.IndexOf("[/size]") + 7));
    
                    strBody = sb.ToString();
                    break;
    
                    //aModBody += strBody.Substring(strBody.IndexOf("[size="), (strBody.IndexOf("[/size]") + 7)) + "<br>";
                    sb.Remove(strBody.IndexOf("[size="), (strBody.IndexOf("[/size]") + 7));
                    //strBody = sb.ToString();
                    //break;
                }
            }
    
            Body = strBody;
            return Body;
        }
    the problem is in the loop, when i dont use the break; statement i get this error:
    Index and length must refer to a location within the string.
    Parameter name: length
    Exception Details: System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.
    Parameter name: length
    Line 67: modBody = strBody.Substring(strBody.IndexOf("[size="), (strBody.IndexOf("[/size]") + 7));


    I dont know why it doesnt continue to the rest of the string!!!!!!!!!!!!!!!!!

  9. #9
    Join Date
    Apr 2005
    Posts
    634
    would this works?
    Code:
    for (int i = 0; i < charBody.Length; i++)
            {
                sb.Append(charBody[i].ToString());
                strBody = sb.ToString();
    
                if (strBody.IndexOf("[size=") != -1 && strBody.IndexOf("[/size]") != -1)
                {
                    // cut the tags and text in tags and put them in other string
                    modBody = strBody.Substring(strBody.IndexOf("[size="), (strBody.IndexOf("[/size]") + 7));
    
                    // modify the tags and replace them with html tags
                    modBody = modBody.Replace("[size=", "<font size=");
                    modBody = modBody.Replace("[/size", "</font");
                    modBody = modBody.Replace("]", ">");
    
      strBody = strBody.Replace(strBody.Substring(strBody.IndexOf("[size="), (strBody.IndexOf("[/size]") + 7)), modBody);
      }//end if
      }//end for

  10. #10
    Join Date
    Oct 2005
    Posts
    252
    Well, it works fine, but i still get the same problem within the loop which in the same line:
    modBody = strBody.Substring(strBody.IndexOf("[size="), (strBody.IndexOf("[/size]") + 7));

    check out this image i took while trying to debug it, the strBody seems correct, i dont know why the substring method dont work in the loop!!!!!!!!!!!!!
    Attached Images Attached Images

  11. #11
    Join Date
    Apr 2005
    Posts
    634
    Quote Originally Posted by Cipher
    i dont know why the substring method dont work in the loop!!!!!!!!!!!!!
    store the return of the subString method into a variable, instead of putting it in a parameter, see if it changes anything....

    1)is it run time error or compile error?
    2)what exactly is the error msg again?

  12. #12
    Join Date
    Oct 2005
    Posts
    252
    What exactly do you mean, isnt this what i'm doing?!
    modBody = strBody.Substring(strBody.IndexOf("[size="), (strBody.IndexOf("[/size]") + 7));

    the modBody is a new string variable i made to store the return of the substring method in.

  13. #13
    Join Date
    Apr 2005
    Posts
    634
    Quote Originally Posted by Cipher
    looks like the problem is at:
    modBody = strBody.Substring(strBody.IndexOf("[size="), (strBody.IndexOf("[/size]") + 7));
    when your input string has additional 7 or more spaces after [/size], then you are fine. One of your ending string was [/size], therefore + 7 will cause error:

    Index and length must refer to a location within the string.

  14. #14
    Join Date
    Oct 2005
    Posts
    252
    hey, i removed the (+7) and still the error remains, i uses so that i can select the end of "[/size ]" tag, other wise it wont select it.
    I'm using variable instead now but ofcourse the same problem
    Code:
                string rtg = "[size=";
                string ltg = "[/size]";
    
                if (strBody.IndexOf(rtg) != -1 && strBody.IndexOf(ltg) != -1)
                {
                    // cut the tags and text in tags and put them in other string
                    modBody = strBody.Substring(strBody.IndexOf(rtg), (strBody.IndexOf(ltg) + ltg.Length));
    Last edited by Cipher; 10-03-2006 at 08:25 PM.

  15. #15
    Join Date
    Apr 2005
    Posts
    634
    the problem is still line under:
    Code:
    modBody = strBody.Substring(strBody.IndexOf(rtg), (strBody.IndexOf(ltg) + ltg.Length));
    to ignore the big picture and just focus on this line of code, i had copied into a console application and run locally to see what happen...

    the following crash at run time:
    Code:
    static void Main(string[] args)
    		{
    			string ltg = "[size]";
    			string rtg = "[/size]";
    			string input = "blah [size] blah blah blah [/size]";
    			Console.Write(input.Substring(input.IndexOf(ltg),input.IndexOf(rtg) + rtg.length));
    
    			Console.ReadLine();
    		}//end main
    then i take those indexof away and put it into a local variable...
    and it is crashing at:
    Code:
    static void Main(string[] args)
    		{
    			string ltg = "[size]";
    			string rtg = "[/size]";
    			string input = "blah [size] blah blah blah [/size]";
    			int start = input.IndexOf(ltg);
    			int end = input.IndexOf(rtg) + rtg.length;
        	Console.Write(input.Substring(start,end));
    			Console.ReadLine();
    		}//end main
    but if you run in debug mode, you'll see start=5, and end = 34.
    now look at my input.length, it says it has size 34....

    but the length starting from [size] to end with [/size] is only 29, so 34 will be out of bounce.

    hope that works for you,
    good luck.
    Last edited by sirpelidor; 10-03-2006 at 11:31 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles