Page 1 of 1

Replacing character pairs

Posted: Fri Aug 05, 2005 5:52 pm
by chulett
I have a field I need to strip pairs of characters from, basically any place I find a caret ^ in the string I need to remove it and the immediately following character. Problem is it's not a small list like only '^R' and '^C', there are 90 different values so something simple like EReplacing them with an empty string isn't feasible.

I know I can find out how many occurances there are in the string with Count and then interate through each chunk using Index, building another copy of the string with these pairs left out... or something like that, anyways. However, before embarking on that boat...

I was wondering if there is a 'better' way to do this, perhaps with Match or something similar? Not having much opportunity to use it yet, I'm not all that familiar with the goofy "0N0X5N..." style pattern matching it needs. :?

I would love to find a single pass way to say - "find every place a caret exists in this string and remove it and the next character". Any way to do this in one swell foop? :wink:

Posted: Fri Aug 05, 2005 7:27 pm
by ray.wurlod
String handling functions are way fast, so a custom routine is probably the easiest way to go.

Code: Select all

FUNCTION ReplaceCharacterPairs(TheString)
If UnAssigned(TheString) Or IsNull(TheString)
Then
   RETURN(@NULL)
End
Ans = ""
Size = Len(TheString)
For Pos = 1 To Size
   TheChar = TheString[Pos,1]
   If TheChar = "^"
   Then
      Pos += 1
   End
   Else
      Ans := TheChar
   End
Next Pos
RETURN(Ans)

Posted: Fri Aug 05, 2005 7:49 pm
by chulett
Good to know they are 'way fast'. :wink:

I was thinking about cutting the string up around the pairs and putting the chunks together, but if iterating through character by character is speedy - you're right, that seems the simplest approach. 8)