Author Topic: Unpad zeroes from string  (Read 2625 times)

Benjamin Dasari

  • GuiXT Forum
  • Newbie
  • *
  • Posts: 95
    • View Profile
Unpad zeroes from string
« on: August 01, 2016, 03:48:42 PM »
Purpose:
To remove zeroes before or after a string based on parameters passed.


Liquid UI Code:

// SAPLSMTR_NAVIGATION.E0100.sjs

// Function to remove zeroes padded to the source based on the direction specified
function unPadZeroes(source,direction) {
   var output = "";
   var sourceLength = 0;
   var sourceOriginalLength = 0;
   source = source.toString();

    if(source) {
       sourceOriginalLength = source.length;   
      sourceLength = source.length;
    }
   
   switch (direction){
      case "LEFT":
         for (var loop = 0; loop < sourceOriginalLength; loop++) {         
            if(source[0]=='0'){
               source = source.substring(1,sourceLength);
               sourceLength = source.length;
            } else {
               break;   
            }
         }
         break;
      case "RIGHT":
         for (var loop = 0; loop < sourceOriginalLength; loop++) {         
            if(source[sourceLength-1]=='0'){
               source = source.substring(0,sourceLength-1);
               sourceLength = source.length;
            } else {
               break;   
            }
         }
         break;
   }
    return source;
}

// Function to remove zeroes to the left of the source
function removeZeroesLeft(){
   z_temp = unPadZeroes(z_value,"LEFT");
   set('V[z_unpadded_value]','&V[z_temp]');
   return;
}

// Function to remove zeroes to the right of the source
function removeZeroesRight(){
   z_temp = unPadZeroes(z_value,"RIGHT");
   set('V[z_unpadded_value]','&V[z_temp]');
   return;
}

// User Interface
clearscreen();
inputfield([1,0], "Enter Value", [1,16], {"size":10, "name":"z_value"});
inputfield([2,0], "New Value", [2,16], {"size":10, "name":"z_unpadded_value", "readonly":true});
pushbutton([4,0], "Remove Zeroes Left", {"process":removeZeroesLeft, "size":[1,18]});
pushbutton([4,20], "Remove Zeroes Right", {"process":removeZeroesRight, "size":[1,18]});


See attachments for code samples!
« Last Edit: August 01, 2016, 05:35:59 PM by Benjamin Dasari »