• Iguana 6
  • Previous Versions
  • API
  • Sample Code
  • Forums
  • Training
  • Create a Ticket
iNTERFACEWARE Help Center
  • Iguana 6
  • Previous Versions
  • API
  • Sample Code
  • Forums
  • 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
scrub.lua
Added by iNTERFACEWARE
Modules
The “scrub” module given below redacts sensitive information from HL7 messages.
sha1.lua
Added by iNTERFACEWARE
Modules
A pure Lua-based implementation of the popular SHA-1 hashing function.
sqlite.lua
Added by iNTERFACEWARE
Modules
A module to create a SQLite database and generate the tables specified in a VMD
store.lua
Added by iNTERFACEWARE
Modules
The "original" store module: Allows you to store key/value pairs in a persistent storage mechanism. We recommend using the new store2 module instead.
store2.lua
Added by iNTERFACEWARE
Modules
Provides a simple interface to store key/value pairs in a persistent storage mechanism.
stream.lua
Added by iNTERFACEWARE
Modules
This module performs basic stream processing, read, write, save to file, convert to text etc.
test_all.lua
Added by iNTERFACEWARE
Modules
Test a script against all the sample messages loaded for the component
throttle.lua
Added by iNTERFACEWARE
Modules
Throttle a process during peak hours, by slowing down the code.
throttleDB.lua
Added by iNTERFACEWARE
Modules
Throttle database access by reducing the number of inserts during peak hours
urlcode.lua
Added by iNTERFACEWARE
Modules
A module for parsing URL encoded GET/POST sequences
validate.lua
Added by iNTERFACEWARE
Modules
A template module for testing HL7 message conformance, you will need to extend it to match your requirements
xml.lua
Added by iNTERFACEWARE
Modules
A collection of helpful XML node functions.
Showing 21 - 32 of 32 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.