Next: 8.4 Variable number of Up: 8 Some Examples Previous: 8.2 The Functions next

8.3 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: 8.4 Variable number of Up: 8 Some Examples Previous: 8.2 The Functions next