node.js - JavaScript String concat replaces first character -


task

after extracting sql statements log file i'm doing simple string concatenation append ; @ end:

var query = line[i] + ";" 

problem

what should like: insert [...];

what like: ;nsert [...]

approaches

i tried different concatenation mechanisms, seeing appended concatenation fails.

for (i in lines) {   var txt = lines[i];   console.log(txt);                 // "insert into"   console.log(txt.concat(";"));     // ";nsert into"   console.log(txt + ";");           // ";nsert into"   console.log(txt+=";");            // ";nsert into"   console.log(";" + txt);           // ";insert into" } 

extraction script

var fs = require('fs'); var array = fs.readfilesync('file').tostring().split("\n"); var result = [];  var currentrow; var line; var value;  // loop through lines of file for(i in array) {     line = array[i];     // if there insert statement, push on array     if (line.lastindexof("insert into", 0) === 0) {       result.push(line);     // if there binding param, interpolate recent insert statement     } else if (line.lastindexof("binding param", 0) === 0){       value = line.split(" - ")[1].replace("\n", "").replace("\r", "");       // hibernate represents null <null> while oracle needs ""       if (value === "<null>") {         value = '""';       // if there string, put "" around       } else if (isnan(value)) {         value = '"' + value + '"';       }       currentrow = result[result.length-1];       // interpolate       currentrow = currentrow.replace("?", value);       result[result.length-1] = currentrow;     } } 

data sneak peek

insert <user>.<table> (<col1>, <col2>, <col3>) values (?, ?, ?) binding parameter [1] [<type>] - <value> binding parameter [2] [<type>] - <value> binding parameter [3] [<type>] - <value> 

system

  • windows 7
  • node.js v4.2.1

question

why ; not appended replaces first character?

as pointy indicated, there terminating carriage return character (ascii 13 decimal) in string.

extending extraction script trim last character if there 1 did trick.

if (line.lastindexof("insert into", 0) === 0) {    if (line.charcodeat(line.length - 1) == 13) {      line = line.substring(0, line.length - 1);    }    result.push(line);    console.log(line); // "insert [...];" } 

note: there prettier solutions using regex.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -