Monday, November 21, 2016

Wfa string, char and numeric functions

Sometimes you need to manipulate strings or numbers in WFA.  You need to extract a character, or add a number.  A lot of those functions are missing.  So here is a library of string, char & number functions I often use.



Add or replace a number

One of those is add or replace padded numbers at the end.  The following function adds numbers to your string.  You must also set a placeholder where it should add the number.
def AddOrReplacePadNumber(name,placeholder,num,padlen) 
{ 
     if(name.contains(placeholder)){
        return name.replaceFirst(placeholder,padNumber(num,padlen))
     }else{
        return name + padNumber(num,padlen);
     }
}

Examples are :
AddOrReplacePadNumber("myvolume","###",1,3)    => myvolume001
AddOrReplacePadNumber("my###volume","###",1,3) => my001volume
AddOrReplacePadNumber("myvolume001","001",2,3) => myvolume002 
Download DAR

Get the last X chars ?

def getLastXChars(sText, num) 
{ 
   int len = sText.length();
   if(num>=len)return sText;
   return sText.substring(len-num,len);
}
Download DAR

Get the first X chars ?

def getFirstXChars(sText, num) 
{ 
   int len = sText.length();
   if(num>=len)return sText;
   return sText.substring(0,num);
}
Download DAR

Get X chars starting from ?

From is starting from 1.
Num is number of chars to return.
If num == 0 then, we assume until end.
If num goes beyond length, we assume until end.
If from is going over length, we assume zero-length string.
def getXChars(sText,from, num) 
{ 
   from--;
   int len = sText.length();
   if(num==0){
      num=len;
   }
   int end = num+from;
   if(from<0){
     throwException("from must be >0 (starting at 1)");
   }
   if(end>len){
     end=len;
   }
   return sText.substring(from,end);
}
Download DAR

Get Substring with direction

Same as previous function, but less checking and assumptions and with direction 'ltr' or 'rtl'
def getSubstringAt(str,position,len,direction) 
{
   if(direction=='rtl'){
     position = str.length()-position -len + 2
   }
     String search = str.substring(position-1,position+len-1);
     return search;
}
Download DAR

Get one char in a string ?

def getCharAt(sText, num) 
{ 
   return sText.substring(num,num+1)
}

Get the number at the end of a string

def GetLastNumber(str) 
{ 
     int len = str.length();
     char c="";
     String s="";
     while(len>0){
         c = str[len-1];
         if("0123456789".contains(c+"")){
             s=c+s;
         }else{
             return s;
         }
         len--;
     }
     return s;
}
Download DAR

Remove some characters from the end of a string ?

def stripLastXChars(sText, num) 
{ 
   return sText.substring(0, sText.length() - num)
}
Download DAR

Replace all.

def replaceAll(name,str,new_str) 
{
   return name.replace(str,(String)new_str);
} 
Download DAR

Round that number ?  

Often used when calculating GB (/1024)
def round(o)
{
   return java.lang.Math.round(o);
} 
Download DAR

Increment a specific part of an IP address

def incrementIpPart(ip,part, increment) 
{
    int p1 = (int)splitByDelimiter(ip,"\\.",0);
    int p2 = (int)splitByDelimiter(ip,"\\.",1);
    int p3 = (int)splitByDelimiter(ip,"\\.",2);
    int p4 = (int)splitByDelimiter(ip,"\\.",3);
    
    if(part==1){
       p1+=increment;
    }else if(part==2){
       p2+=increment;
    }else if(part==3){
       p3+=increment;
    }else if(part==4){
       p4+=increment;
    }
    if(p1>255 || p2>255 || p3>255 || p4>255){
       throwException("Ip overflow")
    }
    return (p1 + "." + p2 + "." + p3 + "." + p4);
}
Download DAR

Is a string in another string ?

def inString(sText,sFindText) 
{ 
     return (sText.contains(sFindText));
}
Download DAR

Random number ?

def randomNumber(minimum, maximum) 
{

  if(maximum > minimum)
    return Math.round((maximum-minimum)*Math.random() + minimum);
  else
    throwException("maximum should be larger than minimum");      

}
Download DAR

Is Node Odd (assuming last 2 chars are node number) ?

def isNodeOdd(sText) 
{ 
   String s = "";
   int i = 0;
   s = sText.substring(sText.length()-2,sText.length());
   i = (Integer.parseInt(s)%2);
   return (i==1);
}
Download DAR

Random password ?

def randomPassword(len) 
{ 
    String passchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~`!@#$%^&*()-_=+[{]}\\|;:,<.>/?";
    int random;
    char randomchar;
    String password ="";
    for(a=1;a<(len+1);a++){
       random = (Math.round((passchars.length()-1)*Math.random()));
       randomchar = passchars.charAt(random);
       password=password+randomchar;
   }
   return password;
}
 

Strip by delimiter

def stripByDelimiter(str, delimiter, indexToStrip) 
{ 
 String result = "";
   String[] splitArray = str.split(delimiter); 
                int max = splitArray.size();
                int i = indexToStrip;
                if(i<max){
                 for(int x=0;x<i;x++){
   result+=splitArray[x] + delimiter;
                 }

            for(int x=(i+1);x<max;x++){
   result+=splitArray[x] + delimiter;
  }
  result = result.substring(0,result.length()-1);
 }else{
  result=str;
 }
 return result;
}

1 comment :

  1. If you need other functions, or you want to share, let me know. I'm planning on creating dar files to download.

    ReplyDelete