-1

I am using the old Borland C++ Builder from around 1998. I often have problems with the string handling. I have not found a good summary of the string routines in the help files. On the Internet I find some information, but the routines quoted are often not found. (Do I fail to include them or are they from later versions??) Right now I have problems with "replace".

#include "string.h"
#include <SysUtils.hpp>
tmpstr = StringReplace( tmpstr, "target", "replacement", TReplaceFlags() );

For example, the code above leads to: "Call to undefined function StringReplace"

Grateful for advice, thanks.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
cvr
  • 161
  • 1
  • 2
  • 10
  • 9
    Maybe a better solution is to upgrade to a compiler that was developed in this decade. – OldProgrammer Jun 15 '14 at 20:06
  • 8
    ..or even in this millennium. – AndersK Jun 15 '14 at 20:37
  • This seems like the time I decided to try out `Pascal hugs`. Generally it was a bad experience. Even with all the hugging ;) – Fantastic Mr Fox Jun 16 '14 at 06:17
  • The language implemented by a 1998 compiler is not what we call C++ today. If you are interested in that language, you will not find much help on the internet. Your best bet is finding printed books from the era. – n. m. could be an AI Jun 16 '14 at 06:53
  • if you have included VCL.h (GUI Form app has it automatically) then the string files are already included and the variable name is `AnsiString` all functions of it are documented just CTRL+F1 when you are on variable or type in source code (at least on all BCB versions I came in contact with) + you can still use the auto complete for fast search ... do not use the String type it is old pascal compatibility wrapper to VCL C++ AnsiString ... – Spektre Aug 10 '14 at 11:21

3 Answers3

0

Based on their documentation here you need to #include , not just SysUtils.hpp or string.h

Artem
  • 7,275
  • 15
  • 57
  • 97
  • Thanks Artem. It is good advice, but I get: Unable to open include file AnsiStrings.hpp – cvr Jun 15 '14 at 20:39
  • Try AnsiStrings or AnsiStrings.h – Artem Jun 15 '14 at 20:41
  • Thanks. It doesn't find the two files you suggest. The only I have made it find is #include But the replace function is still undefined, unfortunately. – cvr Jun 15 '14 at 20:52
  • @Artem you link to documentation of the 2009 edition – M.M Jun 15 '14 at 22:46
  • MattMcNabb Thank you for pointing out. I checked out some source codes and found that the only #include @ycc_swe can you try that? – Artem Jun 16 '14 at 05:51
  • vcl.h automatically includes AnsiString header (which is `dstring.h` but you should not include that directly) – M.M Jun 16 '14 at 05:53
  • #include is already included "automatically" by the compiler. Sorry, I forgot to say that. It thus does not help for StringReplace(... -- Thanks for all comments, guys. – cvr Jun 16 '14 at 08:50
  • Then in this case you might want to make sure you use the right version of StringReplace() by casting your strings to AnsiString (they are chars by default). – Artem Jun 16 '14 at 16:36
  • I am not sure that is possible. My problem is that the compiler does not find StringReplace(...) at all so I can not call the function and try. But for now it runs on [this](http://pastebin.com/NFy2N3fd). Thanks for all advice in this thread. I need to run this compiler a little more before considering a change to a newer. I hope the newer versions of this (Borland) have a short learning period and are not too expensive. C++ seems like a very dynamic, versatile and rapidly developing language. Sometimes too rapidly developing for an older guy, using it from time to time ;) – cvr Jun 17 '14 at 05:04
  • @ycc_swe compiler does not find StringReplace() because signature of it is StringReplace(AnsiString& a, AnsiString& b, ...), not StringReplace(char* a, char* b, ...). Please try StringReplace( AnsiString(tmpstr), AnsiString("target"), AnsiString("replacement"), TReplaceFlags() ); – Artem Jun 17 '14 at 18:28
  • Thank you Artem, I still get "undefined function". Details are [here](http://pastebin.com/F6kPqwBr). But the older compiler works well for my 4 year old HP Pavillion (the hardware of which I like, but not its Win Vista). It is just documentation I need. Searching at home for the books that came with it now. But electronic docs are usually sufficient. Fortunately many example programs included for the event controlled operations. – cvr Jun 18 '14 at 07:43
0

My advice would be to use std::string or std::wstring for all string handling; only converting back to AnsiStrings when you have to interface with VCL components. I wouldn't bother with any AnsiString member functions or free functions which operate on AnsiString.

Consider using a more modern compiler! (There are up-to-date versions of what was formerly Borland C++, although they cost money).

M.M
  • 138,810
  • 21
  • 208
  • 365
  • If it was possible to use other ways than through Ansi strings I would gladly do so. But I don't know how. Some string operations can be done like the following. But I fail to find anything working for "replace/substitute". The following works: String str; segm[1].perp_angle = atof(angle_1->Text.c_str()); // and // str=itvl->Text; int tmitv=StrToInt(str); // and // angle_1->Text=tmpstr.SubString(0,4); // I may have to buy a new compiler, but for the moment I need to solve the string problems. – cvr Jun 16 '14 at 04:08
  • Consult documentation for `std::string`, anything you can do with an AnsiString you can do with standard-compliant code. There's no builtin search-and-replace function, you'd have to search and replace in a loop. – M.M Jun 16 '14 at 04:25
  • As I mentioned, I have problems with the built in documentation. Is there any good source on the Internet for the documentation of what you call std::string. Is std::string the strings declared String s;? Isn't it a little strange not to have a built in replace function? (I then assume there are such functions available in source on the Internet?) I must admit I find it a bit hard to separate Strings, Ansi Strings, Arrays of characters and pointers to characters. – cvr Jun 16 '14 at 04:34
  • `std::string` is part of the C++ standard library. [See here](http://www.cplusplus.com/reference/string/string/). This is different to `String s;`. Regarding the built-in replace there probably isn't one because there's no single version that is optimal in all cases. [See here](http://stackoverflow.com/questions/1494399/how-do-i-search-find-and-replace-in-a-standard-string) for some simple implementations – M.M Jun 16 '14 at 04:46
  • Thanks, I appreciate, but still not there. Also apologize for poor basics, right now I need to get this working before I find a more radical solution. The following two snippets both give "Qualifier 'std' is not a class or namespace name." 1. std::string str = "one two"; -- 2. void myReplace(std::string& str, const std::string& oldStr, const std::string& newStr) ... – cvr Jun 16 '14 at 08:58
  • I tried [this](http://pastebin.com/wMMy0j58) but the compiler replied "Missing ')'" on the first line. I hope I will find something else. But right now C++ doesn't seem like a smooth option. – cvr Jun 16 '14 at 13:50
  • @ycc_swe use a modern compiler and it will be a lot smoother – M.M Jun 17 '14 at 00:40
  • also, get a book to learn from.. you won't get far using trial and error – M.M Jun 17 '14 at 00:41
0

Don't know wchich version You use, but try this instead:

#include <StrUtils.hpp>

extern DELPHI_PACKAGE System::AnsiString __fastcall AnsiReplaceStr(const      System::AnsiString AText, const System::AnsiString AFromText, const System::AnsiString AToText)

Or Type AnsiReplaceStr and F1

Vancalar
  • 963
  • 1
  • 7
  • 15