Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Removing extra spaces and limiting input to A-Z, 1-0, an
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
01159441
Message ID:
01159489
Vues:
16
>>>>I am writing an applications where I want to:
>>>>
>>>>1. remvoe extra spaces between words, i.e. "xxx yyy" becomes "xxx yyy" and that the input is only A-Z and 0-9 (i.e. no specail characters)
>>>>
>>>>2. the input hast the format "xxx.yyy.ccc.etc) where "xxx", "yyy", "ccc", etc can only A-Z, 0-9, last character cannot be a period, and first character of "xxx", "yyy", "ccc", etc is A-Z.
>>>>
>>>>My questions are how to best remove the extra spaces and how check that only A-Z and 0-9 are entered.
>>>>
>>>>TIA
>>>
>>>Claude,
>>>
>>>For removing extra spaces, this should do the trick.
>>>
>>>cString = "xxx  yyy  zzz"
>>>cString = STRTRAN(cString,SPACE(2),SPACE(1),1,OCCURS(SPACE(2),cString))
>>>
>>>Regards,
>>
>>Actually, this would only work if there was only a double space... not a triple or 4 spaces...
>>
>>
>>cString = "xxx     yyy         zzz"
>>do while at( "  ", cString ) > 0
>>  cString = strtran( cString, "  ", " " )
>>enddo
>>
>>
>>
>>As for stripping the other invalid characters you don't want to allow, I would pre-build a string of chars you DONT want, then use CHRTRAN() such as
>>
>>
>>lcInvalid = "`~!@#$%^&*()-_=+{}[]\|;:,<.>/?'" + '"'
>>
>>cString = "xx@()x]  ]  ] y]yy@(*!$         zzz"
>>cString = chrtran( cString, lcInvalid, "" )
>>
>>the CHRTRAN() will take the string, look for each character found in lcInvalid, and replace it with the character in the same position in parameter 3 (which empty will result in no character and thus cut out of the string).
>
>The problem with this approach is that you may know all the valid characters, but the subset of the invalid characters in undetermined...


The sample of invalid characters was just that... a sample... to get a list of all Non A-Z,a-Z,0-9, you could build a loop at the front of your app and keep it defined once... then use that var elsewhere...
lcAllInvalid = ""
for lnI = 1 to 255
  if not upper( chr( lnI )) $ "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    lcAllInvalid = lcAllInvalid + chr( lnI )
  endif
endfor 
This would get even those other special characters at upper levels...

Then, to see if there WERE any invalid characters, you could do...
if len( cString ) <> len( chrtran( cString, lcAllInvalid, "" ))
  */ if string lengths are different, an invalid char was entered
endif 
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform