2

I read content from a file and then write it another file after editing . While editing , for each read line i put a comma at the end of line and then write it to the file. For the last read line i do not want to write a comma. How should i do it.?

 while(!inputFile.AtEndOfStream){
    var readLine =  inputFile.ReadLine();
    var omitChars = new RegExp("#define").test(readLine);
    if(omitChars==1){
        var stringReadPosition = readLine.substr(8);
        var finalString = stringReadPosition.replace(/\s+/g, ': ');
              asd = outputFile.Write(finalString.replace(/^(_)/, "") + ",\r\n");
    }
 }
 outputFile.Write("};\r\n");
 inputFile.Close();
 outputFile.Close();
}
Zee
  • 8,420
  • 5
  • 36
  • 58
vinu
  • 191
  • 2
  • 7
  • 22
  • is this a `javascript` code?? – ozil May 07 '15 at 10:15
  • 1
    possible duplicate of [Javascript chop/slice/trim off last character in string](http://stackoverflow.com/questions/952924/javascript-chop-slice-trim-off-last-character-in-string) – Hexaholic May 07 '15 at 10:15
  • 1
    You could try creating an array and adding outputFile.Write(finalString.replace(/^(_)/, "") as a new element within the while loop. Then outside the loop, use something like [array].join(',\r\n') – Joseph Cape May 07 '15 at 10:19
  • Guambra feo has the perfect answer. Please submit it. – Maarten Bicknese May 07 '15 at 10:22
  • One trick is to output the comma and newline for the previous line at the beginning of the next line, except for the first line (which is easy to identify). – Neil May 07 '15 at 10:25

2 Answers2

2

You can use substring in order to remove the last character of the string after you have formed the string:

finalString = finalString.substring(0, finalString.length - 1);

An alternative would be the use of slice, taking into consideration that negative indices are relative to the end of the string:

finalString  = finalString.slice(0,-1);

Take a look in this question for more.

UPDATE: For your case, you could check whether or not you have reached the EOF - meaning you have read the last line, and then apply slice or substring:

//....
var stringReadPosition = readLine.substr(8);
var finalString = stringReadPosition.replace(/\s+/g, ': ');

//Check if stream reached EOF.
if(inputFile.AtEndOfStream) {
    finalString  = finalString.slice(0,-1);
}
asd = outputFile.Write(finalString.replace(/^(_)/, "") + ",\r\n");
//....
Community
  • 1
  • 1
Nick Louloudakis
  • 5,856
  • 4
  • 41
  • 54
  • The problem is that once it has come out of while loop , it has already written the last line . Now how do i Replace that last line written because answers suggested by you guys is writting the last line again – vinu May 07 '15 at 11:29
  • Answer updated with a relevant addition. Please consider selecting it as best if it is. – Nick Louloudakis May 07 '15 at 13:09
1

try this asd = asd.replace(/,\s*$/, ""); this will remove last comma, I have tried this, its working .Check if it works in ur code. Try this code inside while loop

Rajani Rampelli
  • 179
  • 1
  • 1
  • 12
  • The problem is that once it has come out of while loop , it has already written the last line . Now how do i Replace that last line written because answers suggested by you guys is writting the last line again – vinu May 07 '15 at 10:55