Link to home
Start Free TrialLog in
Avatar of MasterBlaster3000
MasterBlaster3000

asked on

trim all characters except "1234567890" and "+" from a string

hi!
i would like to trim all characters from a string, except "123456789" and "+"...
i need the code in lotus script only...
thx
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

What do you mean with TRIM?

Normally is trim reserved for removing leading and trailing characters.
But from your question I read you like to remove all digits and the plus character from a string.
Is this what you want?

Avatar of AndrewJayPollack
AndrewJayPollack

------------------------------
this is the most flexible way to do it.
by defining the valid characters in a string,
you can change them easily.  The function
does the work so you can use it over and over.
------------------------------
You should up the points to 100 and assign an "A"
for this great work!
------------------------------

const validchars = "123456789+"
originalstring = "a;sldkfja;s218239871a;ksdf"
cleanstring = stripallbutchars(originalstring, validchars)

------------------------------
Uses this function:
------------------------------

Function stripallbutchars(source as string, keepchars as string)as string

dim x as integer
for x = 1 to len(source)
  if instr(source, mid$(source, x, 1)) > 0 then stripallbutchars = stripallbutchars + mid$(source, x, 1)
next x

end function
note: there are technically faster ways to do that function, particularly if you're going to do it thousands of times in a row, or to a REALLY REALLY long text string, however this is the simplest and doing it on a sentence or two (even a paragraph or two) you'd never notice a difference.

One faster way (but less flexible) would be like this, but its hardcoded and less flexible:

originalstring = "a;sldkfja;s218239871a;ksdf"
cleanstring = stripallbutchars(originalstring)

Function stripallbutchars(source as string)as string

dim x as integer
dim a as string
for x = 1 to len(source)next x
  a = mid$(source,x,1)
  select case a
   case "1" : stripallbutchars = stripallbutchars + a
   case "2" : stripallbutchars = stripallbutchars + a
   case "3" : stripallbutchars = stripallbutchars + a
   case "4" : stripallbutchars = stripallbutchars + a
   case "5" : stripallbutchars = stripallbutchars + a
   case "6" : stripallbutchars = stripallbutchars + a
   case "7" : stripallbutchars = stripallbutchars + a
   case "8" : stripallbutchars = stripallbutchars + a
   case "9" : stripallbutchars = stripallbutchars + a
   case "+" : stripallbutchars = stripallbutchars + a
end function
Let me guess, masterblaster3000 (must be from Mad Max, right?) that you're using this for a phone number field, outside the United States?
ASKER CERTIFIED SOLUTION
Avatar of AndrewJayPollack
AndrewJayPollack

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MasterBlaster3000

ASKER

hi Andrew,
yes, your guess was right, it is from madmax AND im using the code for german phone fields(we only have numerical chars in phone numbers)...
im sorry i cannot up the points to 100(i would like too, for sure) since i just got 75 points(but you ill give you an "A" anyway).
thx very much andrew!
thx andrew!