• Iguana 6
  • Previous Versions
  • API
  • Sample Code
  • Training
  • Create a Ticket
iNTERFACEWARE Help Center
  • Iguana 6
  • Previous Versions
  • API
  • Sample Code
  • Training
  • Create a Ticket

Code Repository

Home›Code Repository›stringutil.lua
Modules

stringutil.lua

Verified Featured
Added by iNTERFACEWARE

A library of helpful extensions to the standard Lua string library.

Source Code
-- The stringutil module
-- Copyright (c) 2011-2012 iNTERFACEWARE Inc. ALL RIGHTS RESERVED
-- iNTERFACEWARE permits you to use, modify, and distribute this file in accordance
-- with the terms of the iNTERFACEWARE license agreement accompanying the software
-- in which it is used.
-- http://help.interfaceware.com/code/details/stringutil-lua

-- stringutil contains a number of extensions to the standard Lua String library. 
-- As you can see writing extra methods that will work on strings is very easy. 
-- http://www.lua.org/manual/5.1/manual.html#5.4 for documentation on the Lua String library

-- Trims white space on both sides.
function string.trimWS(self)
   return self:match('^%s*(.-)%s*$')
end

-- Trims white space on right side.
function string.trimRWS(self)
   return self:match('^(.-)%s*$')
end

-- Trims white space on left side.
function string.trimLWS(self)
   return self:match('^%s*(.-)$')
end

-- This routine will replace multiple spaces with single spaces 
function string.compactWS(self) 
   return self:gsub("%s+", " ") 
end

-- This routine capitalizes the first letter of the string
-- and returns the rest in lower characters
function string.capitalize(self)
   local R = self:sub(1,1):upper()..self:sub(2):lower()
   return R
end

local trimWS_help = {
   Title="string.trimWS";
   Usage="string.trimWS(string) or aString:trimWS()",
   SummaryLine="Trims white space from the start and the end of a string.",
   Desc=[[Trims white space from the start and the end of a string.
   ]];
   ["Returns"] = {
      {Desc="String after white spaces have been trimmed <u>string</u>."},
   };
   ParameterTable= false,
   Parameters= {
      {string= {Desc='String to trim spaces from <u>string</u>.'}},
   };
   Examples={
      [[   local S = '   trim spaces before and after   '
   S:trimWS()
   --> 'trim spaces before and after'
      
   string.trimWS('   trim spaces before and after   ') 
   --> 'trim spaces before and after'
   ]],
   };
   SeeAlso={
      {
         Title="stringutil.lua - in our code repository.",
         Link="http://help.interfaceware.com/code/details/stringutil-lua"
      },
      {
         Title="Stringutil – string functions ",
         Link="http://help.interfaceware.com/v6/stringutil-string-functions"
      }
   }
}

help.set{input_function=string.trimWS, help_data=trimWS_help}

local trimRWS_help = {
   Title="string.trimRWS";
   Usage="string.trimRWS(string) or aString:trimRWS()",
   SummaryLine="Trims white space from the right of a string.",
   Desc=[[Trims white space from the right of a string.
   ]];
   ["Returns"] = {
      {Desc="String after white spaces have been trimmed <u>string</u>."},
   };
   ParameterTable= false,
   Parameters= {
      {string= {Desc='String to trim spaces from <u>string</u>.'}},
   };
   Examples={
      [[   local S = '   trim spaces from the right   '
   S:trimRWS()
   --> '   trim spaces from the right'
      
   string.trimRWS('   trim spaces from the right   ') 
   --> '   trim spaces from the right'
   ]],
   };
   SeeAlso={
      {
         Title="stringutil.lua - in our code repository.",
         Link="http://help.interfaceware.com/code/details/stringutil-lua"
      },
      {
         Title="Stringutil – string functions ",
         Link="http://help.interfaceware.com/v6/stringutil-string-functions"
      }
   }
}

help.set{input_function=string.trimRWS, help_data=trimRWS_help}

local trimLWS_help = {
   Title="string.trimLWS";
   Usage="string.trimLWS(string) or aString:trimLWS()",
   SummaryLine="Trims white space from the left of a string.",
   Desc=[[Trims white space from the left of a string.
   ]];
   ["Returns"] = {
      {Desc="String after white spaces have been trimmed <u>string</u>."},
   };
   ParameterTable= false,
   Parameters= {
      {string= {Desc='String to trim spaces from <u>string</u>.'}},
   };
   Examples={
      [[   local S = '   trim spaces from the left   '
   S:trimLWS()
   --> 'trim spaces from the left   '
      
   string.trimLWS('   trim spaces from the left   ') 
   --> 'trim spaces from the left   '
   ]],
   };
   SeeAlso={
      {
         Title="stringutil.lua - in our code repository.",
         Link="http://help.interfaceware.com/code/details/stringutil-lua"
      },
      {
         Title="Stringutil – string functions ",
         Link="http://help.interfaceware.com/v6/stringutil-string-functions"
      }
   }
}

help.set{input_function=string.trimLWS, help_data=trimLWS_help}

local compactWS_help = {
   Title="string.compactWS";
   Usage="string.compactWS(string) or aString:compactWS()",
   SummaryLine="Replace multiple spaces with a single space.",
   Desc=[[Replace multiple spaces in a string with a single space.
   ]];
   ["Returns"] = {
      {Desc="String after white spaces have been trimmed <u>string</u>."},
   };
   ParameterTable= false,
   Parameters= {
      {string= {Desc='String to trim spaces from <u>string</u>.'}},
   };
   Examples={
      [[   local S = '   replace    multiple   spaces   with  a  single  space   '
   S:compactWS()
   --> ' replace multiple spaces with a single space '
      
   string.compactWS('   replace    multiple   spaces   with  a  single  space   ') 
   --> ' replace multiple spaces with a single space '
   ]],
   };
   SeeAlso={
      {
         Title="stringutil.lua - in our code repository.",
         Link="http://help.interfaceware.com/code/details/stringutil-lua"
      },
      {
         Title="Stringutil – string functions ",
         Link="http://help.interfaceware.com/v6/stringutil-string-functions"
      }
   }
}

help.set{input_function=string.compactWS, help_data=compactWS_help}

local capitalize_help = {
   Title="string.capitalize";
   Usage="string.capitalize(string) or aString:capitalize()",
   SummaryLine="Capitalizes the first letter of a string",
   Desc=[[Capitalize the first letter of a string.
   ]];
   ["Returns"] = {
      {Desc="String after it it has been capitalized <u>string</u>."},
   };
   ParameterTable= false,
   Parameters= {
      {string= {Desc='String to be capitalized <u>string</u>.'}},
   };
   Examples={
      [[   local S = 'string to be capitalized'
   S:capitalize()
   --> 'String to be capitalized'
      
   string.capitalize('string to be capitalized') 
   --> 'String to be capitalized'
   ]],
   };
   SeeAlso={
      {
         Title="stringutil.lua - in our code repository.",
         Link="http://help.interfaceware.com/code/details/stringutil-lua"
      },
      {
         Title="Stringutil – string functions ",
         Link="http://help.interfaceware.com/v6/stringutil-string-functions"
      }
   }
}

help.set{input_function=string.capitalize, help_data=capitalize_help}
Description
A library of helpful extensions to the standard Lua string library.
Usage Details

The stringutil.lua module contains a library of helpful extensions to the standard Lua string library.

  • string.trimWS(self): Trims white space on both sides.
  • string.trimRWS(self): Trims white space on right side.
  • string.trimLWS(self): Trims white space on left side.
  • string.compactWS(self): Replace multiple spaces with single spaces.
  • string.capitalize(self): Capitalizes the first letter of the string — and returns the rest in lowercase characters.

Here is some sample code that uses the capitalize() function (works with any sample HL7 message):

require 'stringutil'
-- This module extends the built in string library with a few useful extra functions
-- http://help.interfaceware.com/code/details/stringutil-lua

local Input=[[
<patient firstName="JIM" lastName="bloggs   " title  = " MR " description=''/>
]]

local Description=[[
   Dr   Bob was not   renowned for    using very    consistent spacing.
]]

function main()
   local X = xml.parse{data=Input}
   
   -- Notice we have to convert to strings using :S() before
   -- we can use string functions
   
   -- Capitalize
   X.patient.firstName = X.patient.firstName:S():capitalize()
   
   -- Strip right hand white space then capitalize
   X.patient.lastName = X.patient.lastName:S():trimRWS():capitalize()
   
   -- Strip white space and capitalize
   X.patient.title = X.patient.title:S():trimWS():capitalize()
   
   -- Compact multiple white space and strip leading and trailing space
   X.patient.description = Description:compactWS():trimWS()
end
More Information
• Documentation on the Lua String library
• Tutorials: Transforming Messages: HL7 to HL7
• Tutorials: Transforming Messages: HL7 to JSON
Bookmark
  • Reviews
  • Related Listings
Filter
Sort by: Newest First
  • Oldest First
  • Rating
  • Helpfulness
Write a Review
Rating
Keyword
Filter
Sort by: Title
  • Newest First
  • Oldest First
  • Most Reviews
  • Highest Rated
Rating
iNTERFACEWARE
age.lua
Added by iNTERFACEWARE
Modules
This module calculates age from DOB, it returns years, months and partial years (i.e., 17, 3, 17.296272)
auth.lua
Added by iNTERFACEWARE
Modules
A module that does basic authentication for incoming web requests
batch.lua
Added by iNTERFACEWARE
Modules
A module to help processing batched HL7 messages
codemap.lua
Added by iNTERFACEWARE
Modules
This module is used to map one set of codes to another set of codes, or to validate code membership in a set
csv_parse.lua
Added by iNTERFACEWARE
Modules
A module for parsing well-formed CSV files.
custom_merge.lua
Added by iNTERFACEWARE
Modules
A customizable database merge method for Iguana 5.5.1 and up.
dateparse.lua
Added by iNTERFACEWARE
Modules
A fuzzy date/time parser that is very useful for automatically translating a wide variety of date/time formats.
dup.lua
Added by iNTERFACEWARE
Modules
Duplicate message filter.
edifact.lua
Added by iNTERFACEWARE
Modules
Convert EDI messages to HL7 format so you can process them like an HL7 message (and convert them back to EDI afterwards)
hl7.findSegment.lua
Added by iNTERFACEWARE
Modules
A utility for finding any HL7 segment in a parsed HL7 message node tree.
hl7.serialize.lua
Added by iNTERFACEWARE
Modules
Serializes an HL7 message using specified non-standard delimiters and/or escape characters
hl7.zsegment.lua
Added by iNTERFACEWARE
Modules
Generic Z segment parser. Parses Z segments without needing grammar definitions in the VMD file.
iguanaServer.lua
Added by iNTERFACEWARE
Modules
Provides programmatic access to various operations that can be performed on Iguana channels.
llp.lua
Added by iNTERFACEWARE
Modules
Allows you to use LLP connections from a Translator script
mime.lua
Added by iNTERFACEWARE
Modules
Sends MIME-encoded email attachments using the SMTP protocol. A wrapper around net.smtp.send.
resubmit.lua
Added by iNTERFACEWARE
Modules
Resubmit a logged message to an Iguana channel using the unique reference number (refmsgid).
retry.lua
Added by iNTERFACEWARE
Modules
A module for retrying operations which might periodically fail like database operations.
rtf.lua
Added by iNTERFACEWARE
Modules
A module for converting a RTF file to plain text.
scheduler.lua
Added by iNTERFACEWARE
Modules
Schedule jobs to run at a specified time of day, very useful for batch processing
scrub.lua
Added by iNTERFACEWARE
Modules
The “scrub” module given below redacts sensitive information from HL7 messages.
Showing 1 - 20 of 31 results
«12»

Topics

  • expandGetting Started
  • expandAdministration
    • expandInstallation
    • expandLicensing
    • expandUpgrades
    • expandDeployment
    • expandConfiguration Management
      • expandCustom Configuration
    • expandBackup and Restore
    • expandSecurity
      • expandHIPAA Compliance
    • expandTroubleshooting
  • expandDeveloping Interfaces
    • expandArchitecture
    • expandInterfaces
      • expandHL7
      • expandDatabase
        • expandConnect
      • expandWeb Services
      • expandCDA
      • expandX12
      • expandOther Interfaces
      • expandUtilities
    • expandRepositories
      • expandBuiltin Repositories
        • expandIguana Upgrade
        • expandIguana Tutorials
        • expandIguana Tools
        • expandIguana Protocols
        • expandIguana Files
        • expandIguana Date/Time
        • expandIguana Webservices
        • expandIguana Excel
      • expandRemote Repositories
      • expandCS Team Repositories
        • expandIguana Channels
    • expandSample Code
      • expandModules
      • expandUsing built-in functions
      • expandWorking with XML
    • expandLua Programming
    • expandPerformance
  • expandFAQs and TIPs
    • expandFrequently Asked Questions
      • expandInstalls and Upgrades
      • expandWeb Services
      • expandConfiguration
      • expandChannels
      • expandTranslator
      • expandOther
      • expandDatabase
      • expandAdministration
      • expandLogs
      • expandChameleon
    • expandTips
      • expandChannels
      • expandChameleon
      • expandWeb Services
      • expandSecurity
      • expandProgramming
      • expandOther
      • expandAdministration
  • expandReference
    • expandIguana Enterprise and Professional
    • expandProgram Settings
    • expandChannel Settings
    • expandDashboard
    • expandChannels
    • expandTranslator
    • expandLogs
      • expandLog Encryption
    • expandHTTP API
    • expandCDA API
    • expandError Messages
    • expandChameleon
    • expandIguana Change Log

Other Links

  • Training Center
  • News & Announcements
  • iNTERFACEWARE Blog
  • Older Documention (IGUANA v4 & Chameleon)
Copyright © iNTERFACEWARE Inc.