Applying number format within a text block

TeraBlight
06-10-2005, 08:21 AM
I want to go from a text like this one


"Buy 5000 bricks (6000 if they have that many) for $5 each with a bonus of 25%."


to one like this one


"Buy 5,000 bricks (6,000 if they have that many) for $5.00 each with a bonus of 25%." (not "... bonus of 0.25.")


In other words, I want to reformat the numbers, partly depending on adjacent special characters. Are there any VB features that can help me here, apart from Format() and the InStr family, or will I just have to sit down and write a parser that can handle something like this?

JMorgan
06-10-2005, 08:57 AM
Private Sub Text1_Change()
Form1.Caption = Format(Text1.Text, "$#.00")
End Sub
EDIT: This will be the easiest way as you said in your previous post but i don't think you can do this any other way

DubbleClick
06-10-2005, 11:58 AM
Private Sub Text1_Change()
Form1.Caption = Format(Text1.Text, "$#.00")
End Sub
EDIT: This will be the easiest way as you said in your previous post but i don't think you can do this any other way
You can't use Format() like that.

You will have to parse it.

herilane
06-11-2005, 06:40 AM
For parsing text like this, you will probably find regular expressions useful (tutorial (http://www.xtremevbtalk.com/showthread.php?t=203791)).

TeraBlight
06-11-2005, 08:06 AM
For parsing text like this, you will probably find regular expressions useful (tutorial (http://www.xtremevbtalk.com/showthread.php?t=203791)).

Yep, it would have been useful. However, I wrote a regular parser now which chops up the string into segments (words/numbers/symbols) and then I walk through the segments and apply the format as indicated by the type of the segment and, if any, symbols to the left and right of the segment.
It turned out as only about half a page of code in the end. For some reason I always overestimate the complexity of string manipulation :)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum