Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format String Help

Status
Not open for further replies.

kloner

Programmer
May 15, 2000
79
AU
Hi all,

I'm going crazy! I need to format a formula. I have a string that looks like this "a56c5t78h10K7".

What I need to do is somehow loop through the string and append the html tag <sub></sub> around the integers.

eg final string returned should look like :

Code:
a<sub>56</sub>c<sub>5</sub>t<sub>78</sub>h<sub>10</sub>K<sub>7</sub>

Any ideas or code would be grateful.

Regards kloner
 
I can think of two way off the top of my head,
the first would be to use a regular expression (RegExp object). Give it a search string for integers and use the replace method to replace all integers with the start tage, themselves, and the end tag.
The second way I can think of is to create a blank variable and slowly loop through each character in your string. Also create a boolean which willl start as false.
pseudo-code:
For each character c
if c is a number
if boolean flag = true
newstring = newstring & c
else
newstring = newstring & &quot;<sub>&quot; & c
booleanvar = true
end if
else 'c is a character non-numeric
if boolean flag = true
newstring = newstring & &quot;</sub>&quot; & c
boolean flag = false
else
newstring = newstring & c
end if
endif
if boolean flag = true then newstring = newstring & &quot;</sub>&quot;

Once converted to real code that should work for you, let us know which you end up using

-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top