Results 1 to 6 of 6

Thread: Wrapping Numeric Characters with HTML tags

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Posts
    4

    Wrapping Numeric Characters with HTML tags

    I'm writing a personal simple program that takes text from a textbox and searches it for numeric characters, and if the text has numeric characters, it then asks what HTML tags that should be placed before and after each number, or consecutive set of numbers.

    So if it finds the number 7, it will wrap it with these tags: <span class="ref">7</span>
    If it finds 875, it will wrap only the first and last number like so: <span class="ref">875</span>

    I'm doing this a real stupid way, but I do not really know how to load or size an array on the fly, so what I have going on is this:

    I set the array size according the the length property of the textbox. Then I analyze each character and add 2 spaces to the array when numbers are found so that room can be made for the before and after tags. After the array size is set (which works, but makes things slow, and when large amounts of text are entered it freezes), the program goes through the whole process again, loads the array with the before and after HTML tags in the proper places, empty the textbox, output the array.

    I know this is probably laughable, but I've been playing around with it for about three months in spare time and I'm ready to ask for help, lol. ALL of this below is just to determine the size of the array. It pretty much does the same thing again to load the array, and I know there has to be a better way. This isn't something that needs immediate attention. I'm just learning. Thanks.


    Code:
       'First if statment - validates input
            If txtString.Text = String.Empty Then
                MessageBox.Show("Text field cannot be empty", "Warning",
                         MessageBoxButtons.OK, MessageBoxIcon.Warning)
                txtString.Focus()
            Else
                'Assign the variables and retreive number tags
                strText = CStr(txtString.Text)
    
                'Assign textbox to variable
                strText = CStr(txtString.Text)
                intStringLength = strText.Length
                intArrayLength = intStringLength - 1
    
    
                'THIS WHOLE LOOP DOES NOTHING BUT DETERMINE THE ARRAY SIZE TO BE USED AND DECLARED AFTER DETERMINGING
                'HOW MANY EXTRA PLACES SHOULD BE MADE FOR BEFORE AND AFTER TAGS FOR EACH SET OF NUMBERS
                Do While intCount < intStringLength
                    'Retrieves very first charactor from string
                    strHolder = strText.Substring(intCount, 1)
    
                    'Test characters for numbers
                    If IsNumeric(strHolder) Then
                        blnContainsNumbers = True
                        intArrayLength += 1 'Make room for the before tag in the array
                        Do While IsNumeric(strHolder) = True And blnIsNumeric = True
                            intCount += 1 
                            blnIsNumeric = False
    
                            'If counter is less than string length, get another charactor
                            'This is to prevent an error when only one charactor is entered
                            If intCount < intStringLength Then
                                strHolder = strText.Substring(intCount, 1)
                                If IsNumeric(strHolder) Then
                                    blnIsNumeric = True
                                End If
                            End If
                        Loop
                        intArrayLength += 1 'Make room for last tag in the array
                        intCount += 1
    
                    Else
                        intCount += 1 'Go get another character if it's not the end of the string
                    End If
                Loop
    I took a couple classes in college on VB.NET and these are just the only string manipulation techniques that the book (which was split into two semesters) taught, but when large amounts of text are entered, the program hangs up and freezes. I've tested it every way under the sun, and it works perfectly (except for the slowness and eventually freezing if too much text is entered), except that the last character in the string cannot be numeric, but right now I'm not worried about that.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Wrapping Numeric Characters with HTML tags

    You should use regular expressions. I really should be better with them myself but I have rarely had the need to use them so I've never really developed a proficiency. They are used in .NET via the Regex class.

    You'll first need to find out what the pattern is for a number of arbitrary length. Again, I should know that myself but I don't. It shouldn't be hard to find when you know what you're looking for though.

    You can then use the Regex.Matches method to find all the occurrences of numbers in your text and then use Regex.Replace to replace occurrences of numbers with some other text, which would be your span element containing the original number.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Wrapping Numeric Characters with HTML tags

    edit double post
    Last edited by ident; May 22nd, 2013 at 12:28 PM.

  4. #4
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Wrapping Numeric Characters with HTML tags

    Lets just break this into small steps. You have a textbox that contains a string. It May or may not contain numeric characters. Can we have a few possible examples of these strings? Can they possible contain more then one set of numbers after other chars? Let's assume they dont.

    Example string format: Hello187World

    vb Code:
    1. Dim rx As New Regex("-?\d+", RegexOptions.IgnoreCase Or RegexOptions.Compiled)

    Ok thats easy, now here is where i get confused.

    So if it finds the number 7, it will wrap it with these tags: <span class="ref">7</span>
    If it finds 875, it will wrap only the first and last number like so: <span class="ref">875</span>


    Wrapping the number is fine. But you say if it mates 875 it only wraps the first and last number yet your example wraps the whole number. I will base my example going by whats wrapped above.

    vb Code:
    1. Imports System.Text.RegularExpressions
    2.  
    3. Public Class MainForm
    4.  
    5.     Private Sub NumericInput()
    6.         Dim exampleTextBox1 As String = "Hello187World"
    7.         Dim format As String = "<span class=""ref"">{0}</span>"
    8.         Dim rx As New Regex("-?\d+", RegexOptions.Compiled)
    9.  
    10.         Debug.WriteLine(String.Format(format, rx.Match(exampleTextBox1).Value))
    11.     End Sub
    12.  
    13. End Class

    So now why must you use an array? It will be better to use a collection since we do not know its size & quite possible would want to add to it later.

    vb Code:
    1. Imports System.Text.RegularExpressions
    2.  
    3. Public Class MainForm
    4.  
    5.     Private m_htmlCollection As New List(Of String)
    6.  
    7.     Private Sub NumericInput()
    8.         Dim exampleTextBox1 As String = "123Hello456World789"
    9.         Dim format As String = "<span class=""ref"">{0}</span>"
    10.         Dim rx As New Regex("-?\d+", RegexOptions.IgnoreCase Or RegexOptions.Compiled)
    11.         Dim matches As MatchCollection = rx.Matches(exampleTextBox1)
    12.  
    13.         Me.m_htmlCollection.AddRange(
    14.                                      (
    15.                                       From m
    16.                                       In matches.Cast(Of Match)()
    17.                                       Select (String.Format(format, m.Value))
    18.                                       ).ToArray
    19.                                      )
    20.  
    21.         Array.ForEach(Me.m_htmlCollection.ToArray, AddressOf MessageBox.Show)
    22.     End Sub
    23.  
    24.     Private Sub MainForm_Load(ByVal sender As Object,
    25.                               ByVal e As System.EventArgs) Handles Me.Load
    26.         NumericInput()
    27.     End Sub
    28. End Class

    I hope this can give you some ideas
    Last edited by ident; May 22nd, 2013 at 02:02 PM.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Wrapping Numeric Characters with HTML tags

    Quote Originally Posted by jmcilhinney View Post
    ...regular expressions. I really should be better with them myself but I have rarely had the need to use them so I've never really developed a proficiency...
    Regular Expressions Reference:
    http://www.zvon.org/other/reReference/Output/index.html

    Regex Tutorial:
    http://www.regular-expressions.info/tutorialcnt.html

  6. #6
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Wrapping Numeric Characters with HTML tags


Posting Permissions

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



Click Here to Expand Forum to Full Width