Next: 7.3 Persistence Up: 7 Some Examples Previous: 7.1 The Functions next

7.2 Manipulating Strings

The first example is a function to trim extra blanks at the beginning and end of a string.

function trim(s)
  local l = 1
  while strsub(s,l,l) == ' ' do
    l = l+1
  end
  local r = strlen(s)
  while strsub(s,r,r) == ' ' do
    r = r-1
  end
  return strsub(s,l,r)
end

The second example shows a function that eliminates all blanks of a string.

function remove_blanks (s)
  local b = strfind(s, ' ')
  while b do
    s = strsub(s, 1, b-1) .. strsub(s, b+1)
    b = strfind(s, ' ')
  end
  return s
end


Next: 7.3 Persistence Up: 7 Some Examples Previous: 7.1 The Functions next