Recent

Author Topic: StringReplace  (Read 5131 times)

segfault

  • Full Member
  • ***
  • Posts: 107
StringReplace
« on: March 26, 2019, 03:36:37 pm »
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  [Select][+][-]
  1. for i := 1 to 53 do  // 53 reserved words
  2.   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  [Select][+][-]
  1. const
  2.   reserved : array[1..53] of string = ('absolute', 'and', 'array', 'asm', 'begin', ...)
  3.  

Not sure why, is it because StringReplace is expecting constants as the 2nd and 3rd parameters?

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: StringReplace
« Reply #1 on: March 26, 2019, 03:57:14 pm »
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?
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

segfault

  • Full Member
  • ***
  • Posts: 107
Re: StringReplace
« Reply #2 on: March 26, 2019, 04:48:46 pm »
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  [Select][+][-]
  1. USES sysutils;
  2. {CONST
  3.   reserved : ARRAY[1..53] OF string = (
  4. 'absolute',
  5. 'and',
  6. 'array',
  7. 'asm',
  8. 'begin',
  9. 'case',
  10. 'const',
  11. 'constructor',
  12. 'destructor',
  13. 'div',
  14. 'do',
  15. 'downto',
  16. 'else',
  17. 'end',
  18. 'file',
  19. 'for',
  20. 'function',
  21. 'goto',
  22. 'if',
  23. 'implementation',
  24. 'in',
  25. 'inherited',
  26. 'inline',
  27. 'interface',
  28. 'label',
  29. 'mod',
  30. 'nil',
  31. 'not',
  32. 'object',
  33. 'of',
  34. 'operator',
  35. 'or',
  36. 'packed',
  37. 'procedure',
  38. 'program',
  39. 'record',
  40. 'reintroduce',
  41. 'repeat',
  42. 'self',
  43. 'set',
  44. 'shl',
  45. 'shr',
  46. 'string',
  47. 'then',
  48. 'to',
  49. 'type',
  50. 'unit',
  51. 'until',
  52. 'uses',
  53. 'var',
  54. 'while',
  55. 'with',
  56. 'xor');}
  57.  
  58. VAR
  59.   st  :  string;
  60.   i   :  integer;
  61.   fn  :  text;
  62.   reserved : ARRAY[1..53] OF string;
  63. BEGIN
  64.   // load the file of reserved words into array 'reserved'
  65.   assign(fn, 'reserved.txt');
  66.   reset(fn);
  67.   FOR i := 1 TO 53 DO BEGIN
  68.     readln(fn, st);
  69.     reserved[i] := st;
  70.   END;
  71.   close(fn);
  72.  
  73.   writeln('Input a line of code: ');
  74.   readln(st);
  75.   FOR i := 1 TO 53 DO
  76.     st := StringReplace(st, reserved[i], upcase(reserved[i]), [rfReplaceAll]);
  77.   writeln(st);
  78. END.
  79.  

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

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: StringReplace
« Reply #3 on: March 26, 2019, 05:01:27 pm »
Maybe, try to call AdjustLineBreaks() on the text and see if that helps.

Bart

furious programming

  • Hero Member
  • *****
  • Posts: 852
Re: StringReplace
« Reply #4 on: March 26, 2019, 05:05:15 pm »
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.
« Last Edit: March 26, 2019, 05:06:48 pm by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: StringReplace
« Reply #5 on: March 26, 2019, 05:12:39 pm »
So use Trim(Reserved[ i ]) then.

Bart

segfault

  • Full Member
  • ***
  • Posts: 107
Re: StringReplace
« Reply #6 on: March 26, 2019, 06:39:02 pm »
Thanks guys, works ok now.

Zvoni

  • Hero Member
  • *****
  • Posts: 2317
Re: StringReplace
« Reply #7 on: March 26, 2019, 06:39:31 pm »
So use Trim(Reserved[ i ]) then.

Bart

Or save the reserved words in one line as comma-delimited, and load it into a StringList
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

m.abudrais

  • Jr. Member
  • **
  • Posts: 52
Re: StringReplace
« Reply #8 on: March 26, 2019, 08:18:17 pm »
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.
which editor did you use to show line ending char? is't gedit?

Zvoni

  • Hero Member
  • *****
  • Posts: 2317
Re: StringReplace
« Reply #9 on: March 26, 2019, 10:12:32 pm »
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.
which editor did you use to show line ending char? is't gedit?
Wild Guess: Geany
Maybe Notepad++
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

furious programming

  • Hero Member
  • *****
  • Posts: 852
Re: StringReplace
« Reply #10 on: March 26, 2019, 10:13:20 pm »
This is Notepad++ with Mono Industrial skin, but I found the trailing spaces during debugging the example code.
« Last Edit: March 26, 2019, 10:15:09 pm by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

m.abudrais

  • Jr. Member
  • **
  • Posts: 52
Re: StringReplace
« Reply #11 on: March 27, 2019, 03:06:43 pm »
This is Notepad++ with Mono Industrial skin, but I found the trailing spaces during debugging the example code.
thank you,it's a  helpful  feature.

wp

  • Hero Member
  • *****
  • Posts: 11854
Re: StringReplace
« Reply #12 on: March 27, 2019, 03:17:30 pm »
BTW, the Lazarus IDE can do the same: right click on editor window > Options > Editor > General > Miscellanious > Check "Show special characters"

Useful, for example, to detect TAB characters when the same file is edited by several users not following the same convention how the TAB key should be handled which results in awful indentation style.

m.abudrais

  • Jr. Member
  • **
  • Posts: 52
Re: StringReplace
« Reply #13 on: March 27, 2019, 03:46:40 pm »
BTW, the Lazarus IDE can do the same: right click on editor window > Options > Editor > General > Miscellanious > Check "Show special characters"

Useful, for example, to detect TAB characters when the same file is edited by several users not following the same convention how the TAB key should be handled which results in awful indentation style.
is there option to show line ending char? it only show space and tab.

wp

  • Hero Member
  • *****
  • Posts: 11854
Re: StringReplace
« Reply #14 on: March 27, 2019, 03:56:48 pm »
is there option to show line ending char?
No

 

TinyPortal © 2005-2018