String, Extract Substring
String, Extract Substring

Extracts N characters from the beginning. If negative, N characters (absolute value) are extracted from the end. When the length of the original string is shorter than N, returns the original. To be padded to N, set the pad string.

(C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/string-extract-substring/

Configs
  • A: Set String * #{EL}
  • B: Set Length N of Substring (eg. “3” “-5” ) * #{EL}
  • C: To return fixed length N, Set Pad String (eg “*” “0”) #{EL}
  • D: Select STRING DATA for Substring (update) *
Script (click to open)
// GraalJS (engine type: 2)
// 
// Notes:
// Similar to Excel's LEFT function or JavaScript's Slice function.
// Can be used to cut out the beginning of long texts.
// Can be used for pre-processing to replace emails or phone numbers with asterisks.
// - Use [Update Data] for character string combination. (M227)
// - In some cases, #sformat function (format by "java.lang.String") can be substituted.
//
// Notes (ja):
// エクセルの LEFT 関数や JavaScript の Slice 関数と同等の処理を実現できます。
// テキストのリード文を切り出す場合などに活用できます。
// 伏字加工(メールアドレスや電話番号等)の前処理にも活用できます。
// - 文字列の結合処理には、[データ更新]を利用します(M227)
// - #sformat 関数("java.lang.String" による format )で代用可能なケースもあります


//////// START "main()" ////////////////////////////////////////////////////////////////
main();
function main(){

//// == Config Retrieving / 工程コンフィグの参照 ==
const strA    = configs.get( "conf_StrA" )   + "";   // required
const numB    = configs.get( "conf_NumB" )   + "";   // required
const strPad  = configs.get( "conf_StrPad" ) + "";   // not required
const dataIdD = configs.get( "conf_DataIdD" )  + ""; // required
if( isNaN( parseInt(numB) )){
  throw new Error( "\n AutomatedTask ConfigError:" +
                   " Config {B} is not an integer \n" );
}
engine.log( " AutomatedTask Substring Length: " + numB );
engine.log( " AutomatedTask Padding String: " + strPad );


//// == Data Retrieving / ワークフローデータの参照 ==
// (nothing, except Expression Language config)


//// == Calculating / 演算 ==
let strOutput = "";
if( 0 < numB && numB < strA.length ){
  strOutput += strA.slice( 0, numB );
}else if( strA.length < numB ){
  strOutput += strA;
  if( strPad !== "" ){
    strOutput = myPadEnd( strOutput, numB, strPad );
  }
}else if( numB < 0 && -numB < strA.length ){
  strOutput += strA.slice( numB );
}else if( strA.length < -numB ){
  strOutput += strA;
  if( strPad !== "" ){
    strOutput = myPadStart( strOutput, -numB, strPad );
  }
}


//// == ワークフローデータへの代入 / Data Updating ==
engine.setDataByNumber( dataIdD, strOutput );

} //////// END "main()" ////////////////////////////////////////////////////////////////


function myPadEnd( strTarget, numLength, strPad ){
  if( strTarget.length >= numLength ){
    return strTarget;
  }else{
    numLength = numLength - strTarget.length;
    if( numLength > strPad.length ){
      strPad += strPad.repeat( numLength / strPad.length );
    }
    return strTarget + strPad.slice( 0, numLength );
  }
}
function myPadStart( strTarget, numLength, strPad ){
  if( strTarget.length >= numLength){
    return strTarget;
  }else{
    numLength = numLength - strTarget.length;
    if( numLength > strPad.length ){
      strPad += strPad.repeat( numLength / strPad.length );
    }
    return strPad.slice( 0, numLength ) + strTarget;
  }
}

Download

Capture

Extracts N characters from the beginning. If negative, N characters (absolute value) are extracted from the end. When the length of the original string is shorter than N, returns the original. To be padded to N, set the pad string.

Notes

  1. Similar to Excel’s LEFT function or JavaScript’s Slice function.
  2. Can be used to cut out the beginning of long texts.
  3. Can be used for pre-processing to replace emails or phone numbers with asterisks.
    1. Use [Update Data] for character string combination. (M227)
    2. In some cases, #sformat function (format by “java.lang.String”) can be substituted.
  4. Substring Length = “5”, Padding String = “”
    • 1234567890 → 12345
    • 123 → 123
  5. Substring Length = “5”, Padding String = “0”
    • 1234567890 → 12345
    • 123 → 12300
  6. Substring Length = “-5”, Padding String = “0”
    • 1234567890 → 67890
    • 123 → 00123
  7. Substring Length = “3”, Padding String = “*”
    • example@example.com → exa
    • a → a**
  8. Substring Length = “-7”, Padding String = “*”
    • example@example.com → ple.com
    • a → ******a
  9. Substring Length = “-12”, Padding String = “foobar”
    • example@example.com → @example.com
    • a → foobarfoobaa

See also

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: