Handling backspace chars in a string...

Purple bwizard at bga.com
Sun Apr 25 19:01:34 EDT 1999


I'm in the posistion of having to process strings with arbitrary
numbers of backspace and newline characters in them. The backspaces
actually get put in the string, so I have to handle removing the
characters that are backspaced over. Currently I'm doing this
something like this (hastily retyped mostly from memory so forgive any
small errors and/or typos) :

         i = len(str)
         while ktr < i:
            if string[ktr] == '\b':
               bBegin = ktr
               backs = 0
               while string[ktr] == '\b':
                  backs = backs + 1
                  ktr = ktr + 1
               if backs > (ktr - backs - 1): # backs > prior chars
                  string = string[bBegin+backs:]
                  ktr = 0
               else:
                  string = string[:bBegin-backs] + string[ktr:]
                  ktr = bBegin - backs
               i = len(str)
            ktr = ktr + 1

This just looked rather messy to me -- I was curious if anyone know a
better way?





More information about the Python-list mailing list