Author Topic: Padding a String  (Read 3831 times)

Benjamin Dasari

  • GuiXT Forum
  • Newbie
  • *
  • Posts: 95
    • View Profile
Padding a String
« on: July 27, 2016, 04:16:06 PM »
Purpose:
To add characters before or after a string based on parameters passed.


Liquid UI Code:

// SAPLSMTR_NAVIGATION.E0100.sjs

// Remove blank spaces
String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,'');}

const PADDING_LEFT = 0;
const PADDING_RIGHT = 1;

// Function to add characters before or after the source based on the parameters
function padString(source,length,direction,character) {
   var loop;
   var output = "";
   var sourceLength = 0;
   if (typeof(source) != 'string'){
      source = source.toString();
   }

   set('V[z_source]',source.trim());
   if(z_source) {
      sourceLength = z_source.length;
   }
   
   switch(direction) {
   case PADDING_LEFT:
      for(loop = 0; loop < (length - sourceLength); loop++) {
         output += character;
      }
      output = output + z_source;

      break;
      
   case PADDING_RIGHT:
      for(loop = 0; loop < (length - sourceLength); loop++) {
         output += character;
      }
      output = z_source + output;
      break;
   }
   return output;
}

// Function to return the padded value based on the character value passed
function padValueWithChar(param){
   z_temp = padString(z_value,10,0,param.mychar);
   set('V[z_padded_value]','&V[z_temp]');
   return;
}

// User Interface
clearscreen();
inputfield([1,0], "Enter Value", [1,16], {"size":10, "name":"z_value"});
inputfield([2,0], "After Padding", [2,16], {"size":10, "name":"z_padded_value", "readonly":true});
pushbutton([3,0], "Pad Value", {"process":padValueWithChar, "using":{"mychar":"0"}});


See attachments for code samples!
« Last Edit: August 01, 2016, 03:47:08 PM by Benjamin Dasari »