文字列, 部分文字列の抽出
文字列, 部分文字列の抽出 (String, Extract Substring)

先頭N文字を切り取ります。Nに負の値が指定された場合は、末尾N文字(絶対値)を切り出します。元の文字列の長さがNより短い場合には、元の文字列をそのまま取得します。もし、取得文字列の長さをN文字に伸張する必要がある場合は、パディング文字(穴埋め文字)を設定します。

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

Configs
  • A: 文字列をセットしてください * #{EL}
  • B: 切り取る文字列(部分文字列)の長さNをセットしてください (例 “3” “-5” ) * #{EL}
  • C: N文字の固定長で返したい場合はパディング文字をセットしてください (例 “*” “0” ) #{EL}
  • D: 部分文字列が格納される文字列型データ項目を選択してください (更新) *
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

先頭N文字を切り取ります。Nに負の値が指定された場合は、末尾N文字(絶対値)を切り出します。元の文字列の長さがNより短い場合には、元の文字列をそのまま取得します。もし、取得文字列の長さをN文字に伸張する必要がある場合は、パディング文字(穴埋め文字)を設定します。

Notes

  1. エクセルの LEFT 関数や JavaScript の Slice 関数に近い処理が実現できます。
  2. テキストのリード文を切り出す場合などに活用できます。
  3. 伏字加工(メールアドレスや電話番号等)の前処理にも活用できます。
    1. 文字列の結合処理には、[データ更新]を利用します(M227
    2. #sformat 関数(”java.lang.String” による format )で代用可能なケースもあります
  4. 切出文字数 = “5”、パディング文字 = (未設定)
    • 1234567890 ⇒ 12345
    • 123 ⇒ 123
  5. 切出文字数 = “5”、パディング文字 = “0”
    • 1234567890 ⇒ 12345
    • 123 ⇒ 12300
  6. 切出文字数 = “-5″、パディング文字 = “0”
    • 1234567890 ⇒ 67890
    • 123 ⇒ 00123
  7. 切出文字数 = “3”、パディング文字 = “*”
    • example@example.com ⇒ exa
    • a ⇒ a**
  8. 切出文字数 = “-7″、パディング文字 = “*”
    • example@example.com ⇒ ple.com
    • a ⇒ ******a
  9. 切出文字数 = “-12″、パディング文字 = “foobar”
    1. example@example.com ⇒ @example.com
    2. a ⇒ foobarfoobaa

See also

コメントを残す

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください

%d人のブロガーが「いいね」をつけました。