Re: Need help with this

new topic     » goto parent     » topic index » view thread      » older message » newer message

C Bouzy wrote:

> I have been attempting to write a function that removes any amount of 
> numbers from the beginning of a string of text, but it is not working
> correctly. Anyone want to take a crack at this?
> 
> Example:  01-MyText  = MyText
> Example:   01 - MyText = MyText
> The hyphen after the numbers is important, because if there is no hyphen
> after the numbers, the numbers should not be removed. 

Maybe this is what you want:

function verify (integer start, sequence source, sequence charlist)
   -- searches for the first character in 'source', that is not contained
   -- in 'charlist', beginning at position 'start';
   -- returns the position of that character, or 0 if nothing found
   -- in: start   : 1..length(source)
   --     source  : empty sequence ==> function returns 0
   --     charlist: empty sequence ==> function returns 0

   if start >= 1 and length(charlist) then
      for i = start to length(source) do
         if not find(source[i], charlist) then
            return i
         end if
      end for
   end if
   return 0
end function


constant
   NUMERIC = "0123456789.",
   WHITESPACE = "\t\r\n "

global function special_func (sequence source)
   integer p
   
   p = verify(1, source, NUMERIC)
   if p != 0 then
      p = verify(p, source, WHITESPACE)
      if p != 0 and source[p] = '-' then
         p = verify(p+1, source, WHITESPACE)
         if p != 0 then
            return source[p..$]
         end if
      end if
   end if
   return source
end function


-- * Demo * --
printf(1, "'%s'\n", {special_func("01-MyText")})
printf(1, "'%s'\n", {special_func("01 - MyText")})
printf(1, "'%s'\n", {special_func("01  MyText")})
printf(1, "'%s'\n", {special_func(" MyText")})


Regards,
   Juergen

-- 
Have you read a good program lately?

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu