Axaptapedia is now maintained by AgileCadence For more information please click here.

String validation

From Axaptapedia
Jump to: navigation, search

see also Regular expressions

How to check if a string includes only authorized characters[edit]

I had a few difficulties to find any function to compare a string against a pattern finally I've wrote a very simple method.

You can put it in an object or in a class for an external general use.

<xpp>public boolean CheckCharsInString(str _str) {

   str         char;
   int         pos;
   int         i;
   container   BadChar = [';', '.',':','/','\\','!','?','-','_','&','@','\,'$','£','µ','*','%','ù','§','+','=','}','{',']','[',')','(','|','"','~','²','€'];
   for (pos=0;pos<=strlen(_str);pos++) {
       char=substr(_str,pos,1);
       for (i=0;i<=conlen(BadChar);i++){
           if (char == conpeek(BadChar,i)){
               return(false);
           }
       }
   }
   return(true);

}</xpp>

Ax 4.0 offers two new functions strRem() and strKeep() for this task: <xpp> ...

   str a = strRem("Hello World!", "eo"); // removes the characters "e" and "o" from given string
   str b = strKeep("Hello World!", "eo"); // removes everything, except for chars "e" and "o" from given string

... </xpp>

Note: See the discussion for a few comments about this page regarding easier ways to achive string validation.