Strip or add leading zeros
Verified
Added by iNTERFACEWARE
Simple functions to strip or add leading zeros
Source Code
-- following best practice by putting local -- functions first before main() local function addZeros(Id, length) Id = string.rep('0', length-#Id)..Id return Id end local function removeZeros(Id) while true do if Id:sub(1,1) == '0' then Id = Id:sub(2) else break end end return Id end function main() local Id1 = removeZeros('0004589787') local Id2 = addZeros('4589787', 10) trace(Id1) trace(Id2) end
Description
Simple functions to strip or add leading zeros
Usage Details
This code uses two functions removeZeros()
and addZeros()
to strip or add leading zeros from a string.
How to use this code:
- Simply paste the code into any Lua script component
- Play around with it to see how it works, try changing the strings
- See if you can modify it work with numbers
Hint: Try usingtonumber()
andtostring()
or:S()
More Information
Added by iNTERFACEWARE
How to generate an HL7 message with non-standard delimiters and/or escaping, using serialize.lua
Added by iNTERFACEWARE
How to prevent escaping of special characters like "&" in an HL7 message
Added by iNTERFACEWARE
How to format an HL7 message without the trailing "|" at the end of each line
Added by iNTERFACEWARE
Basic example of merging data into a SQLite database (code is "plug and play" as the DB will be created if it does not exist)
Added by iNTERFACEWARE
Demonstrates the best way to set a XML node to a space character, by using setText(' ') from the xml.lua module
Added by iNTERFACEWARE
Demonstrates the best way to read an empty XML TEXT element, by using the text(' ') function from the xml.lua module
Added by iNTERFACEWARE
How to map all or part of an HL7 message (similar principles apply to other message types)
Added by iNTERFACEWARE
How to transform part of an HL7 message (similar principles apply to other message types)
Added by iNTERFACEWARE
How to filter out unwanted HL7 messages (similar principles apply to other message types)
Added by iNTERFACEWARE
How to insert data from an HL7 message into a database (similar principles apply to other message types)
Added by iNTERFACEWARE
How to update a database using data from an HL7 message (similar principles apply to other message types)
Added by iNTERFACEWARE
A very simple way to run all your code in protected mode with pcall() and log errors instead of stopping
Added by iNTERFACEWARE
Shows the correct way to add and update TEXT (and other) fields, as well as what not to do
Added by iNTERFACEWARE
How to correctly escape a SQL query string
Added by iNTERFACEWARE
How to handle inserts and updates for a table using an autoincrementing ID
Added by iNTERFACEWARE
How to dehydrate (serialize) raw JSON data, this can be useful for storing in a databases
Added by iNTERFACEWARE
How to dehydrate (serialize) raw XML data, this can be useful for storing in a databases