Forum > General

StringReplace

(1/4) > >>

segfault:
I've written a little program to replace all the reserved words in some code by their uppercase equivalents. I opened a text file with a list of all the reserved words and put them into an array, then for each reserved word used stringreplace for each line in the code. ie


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---for i := 1 to 53 do  // 53 reserved words  st := StringReplace(st, reserved[i], upcase(reserved[i], [rfReplaceAll]);
However, this didn't work. It only worked when instead of loading the reserved words from a file I put them "manually" into the source, ie


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---const  reserved : array[1..53] of string = ('absolute', 'and', 'array', 'asm', 'begin', ...) 
Not sure why, is it because StringReplace is expecting constants as the 2nd and 3rd parameters?

Mike.Cornflake:
Don't think the problem is with StringReplace (because you prove that works yourself by hardcoding your array).  It's likely in your code that loads reserved.  Can you show us that code instead?

segfault:
Here's some code which does the same thing except it prompts you to enter a string from the console. I've included the hard-coded array which is commented out.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---USES sysutils;{CONST  reserved : ARRAY[1..53] OF string = ('absolute','and','array','asm','begin','case','const','constructor','destructor','div','do','downto','else','end','file','for','function','goto','if','implementation','in','inherited','inline','interface','label','mod','nil','not','object','of','operator','or','packed','procedure','program','record','reintroduce','repeat','self','set','shl','shr','string','then','to','type','unit','until','uses','var','while','with','xor');} VAR  st  :  string;  i   :  integer;  fn  :  text;  reserved : ARRAY[1..53] OF string;BEGIN  // load the file of reserved words into array 'reserved'  assign(fn, 'reserved.txt');  reset(fn);  FOR i := 1 TO 53 DO BEGIN    readln(fn, st);    reserved[i] := st;  END;  close(fn);   writeln('Input a line of code: ');  readln(st);  FOR i := 1 TO 53 DO    st := StringReplace(st, reserved[i], upcase(reserved[i]), [rfReplaceAll]);  writeln(st);END. 
I've also attached the reserved.txt file. Actually I think the problem might be with the format of the file. I cut and pasted the list of reserved words directly from the FP site so I think there may be an issue with the end-of-line markers.

Bart:
Maybe, try to call AdjustLineBreaks() on the text and see if that helps.

Bart

furious programming:
Look at this file — there are trailing white characters, from the case to the very end (look at the attachment). Delete these characters and the code will work as you expect.

Navigation

[0] Message Index

[#] Next page

Go to full version