|
-
May 22nd, 2013, 03:38 AM
#1
Thread Starter
New Member
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.
-
May 22nd, 2013, 04:00 AM
#2
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.
-
May 22nd, 2013, 12:17 PM
#3
Re: Wrapping Numeric Characters with HTML tags
Last edited by ident; May 22nd, 2013 at 12:28 PM.
-
May 22nd, 2013, 12:27 PM
#4
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:
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:
Imports System.Text.RegularExpressions Public Class MainForm Private Sub NumericInput() Dim exampleTextBox1 As String = "Hello187World" Dim format As String = "<span class=""ref"">{0}</span>" Dim rx As New Regex("-?\d+", RegexOptions.Compiled) Debug.WriteLine(String.Format(format, rx.Match(exampleTextBox1).Value)) End Sub 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:
Imports System.Text.RegularExpressions Public Class MainForm Private m_htmlCollection As New List(Of String) Private Sub NumericInput() Dim exampleTextBox1 As String = "123Hello456World789" Dim format As String = "<span class=""ref"">{0}</span>" Dim rx As New Regex("-?\d+", RegexOptions.IgnoreCase Or RegexOptions.Compiled) Dim matches As MatchCollection = rx.Matches(exampleTextBox1) Me.m_htmlCollection.AddRange( ( From m In matches.Cast(Of Match)() Select (String.Format(format, m.Value)) ).ToArray ) Array.ForEach(Me.m_htmlCollection.ToArray, AddressOf MessageBox.Show) End Sub Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load NumericInput() End Sub End Class
I hope this can give you some ideas
Last edited by ident; May 22nd, 2013 at 02:02 PM.
-
May 22nd, 2013, 12:40 PM
#5
Re: Wrapping Numeric Characters with HTML tags
 Originally Posted by jmcilhinney
...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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 22nd, 2013, 01:58 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|