String, Get Number of Characters

String, Get Number of Characters
String, Get Number of Characters

Gets the number of characters in the text. Consecutive blanks (including tabs and double-byte spaces) are counted as one character. The number of characters that do not count linefeed codes and blanks can also be obtained.

2020-03-24 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/string-get-number-of-characters/

Configs
  • A: Set String * #{EL}
  • B: Select NUMERIC DATA for Number of Characters (update)
  • C: Select NUMERIC for #Characters ignoring LF (update)
  • D: Select NUMERIC for #Characters ignoring LF and Space (update)
Script
// Nashorn Script (engine type: 1)
// 
// Notes:
// Counts consecutive spaces (including tabs and double-byte spaces) as one character.
// Use C if you do not want to count linefeed codes as characters.
// Use D if you do not want to count linefeed codes and spaces as characters.
// 
// Notes (ja):
// 連続する空白(タブや全角空白も含む)は全体で1文字とカウントされます。
// 改行コードを文字数にカウントしたくない場合はCを使用してください。
// 改行コードと空白を文字数にカウントしたくない場合はDを使用してください。


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

//// == Config Retrieving / 工程コンフィグの参照 ==
const strA    = configs.get( "conf_StrA" )    + ""; // required
const dataIdB = configs.get( "conf_DataIdB" ) + ""; // not required
const dataIdC = configs.get( "conf_DataIdC" ) + ""; // not required
const dataIdD = configs.get( "conf_DataIdD" ) + ""; // not required
if( strA === "" ){
  engine.log( " AutomatedTask StringWarning:" +
              " Config {A} is empty" );
}


//// == Data Retrieving / ワークフローデータの参照 ==
// (nothing)


//// == Calculating / 演算 ==
const strB = strA.replace(/\s+/g," ");
const strC = strA.replace(/\r|\n/g, "").replace(/\s+/g," ");
const strD = strA.replace(/\s/g,"");
// Note) Tab and double byte space are included in "\s" (UTF-8) 
// including space, tab, form feed, line feed, and other Unicode spaces.

const numB = strB.length;
const numC = strC.length;
const numD = strD.length;


//// == ワークフローデータへの代入 / Data Updating ==
if( dataIdB !== "" ){
  engine.setDataByNumber( dataIdB, new java.math.BigDecimal( numB ) );
}
if( dataIdC !== "" ){
  engine.setDataByNumber( dataIdC, new java.math.BigDecimal( numC ) );
}
if( dataIdD !== "" ){
  engine.setDataByNumber( dataIdD, new java.math.BigDecimal( numD ) );
}


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

Download

Capture

Gets the number of characters in the text. Consecutive blanks (including tabs and double-byte spaces) are counted as one character. The number of characters that do not count linefeed codes and blanks can also be obtained.

Notes

  1. Counts consecutive spaces (including tabs and double-byte spaces) as one character.
  2. Use C if you do not want to count linefeed codes as characters.
  3. Use D if you do not want to count linefeed codes and spaces as characters.

See also

Leave a Reply

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

%d bloggers like this: