'=========================================================================== ' Subject: REPLACE A STRING Date: 01-21-99 (08:36) ' Author: Hermann Seegert Code: PB ' Origin: Hermann.Seegert@t-online.de Packet: PB.ABC '=========================================================================== $IF 0 ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ Ü ³ ³ Û ³ Programm : REPLACE.BAS ³ Û ³ Autor (c) : Hermann Seegert ³ Û ³ Date : Sept. 1998 ³ Û ³ Language : PowerBASIC, version 3.2 ³ Û ³ EMail : hermann.seegert@t-online.de ³ Û ³ Status : FREEWARE ³ Û ³ FunKtion : It's a routine to replace a string, but it doesn't ³ Û ³ matter, how the original string is written (in ³ Û ³ opposite to the REPLACE- task from PB !). ³ Û ³ Any remarks- perhaps improvements- can be sent to ³ Û ³ my email - address. Thanks. ³ Û ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ Û ßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßß $ENDIF ' 'Perhaps you think: Why a REPLACE- task for PowerBASIC, PowerBASIC 'has a task named REPLACE, and this task is working very well. Of 'course, it does, I wouldn't deny that, but the PowerBASIC- task is 'not capable to solve the problem of replacing a string with an 'other string, if you don't want to care of the way how the original 'string is written. And this actualy does my procedure SUB repla- 'ced(...). May be, you can improve it; it's not the quickest, but I ' can not programme in assembler. Anyway: It works ! 'It's FREEWARE of course, if you want to send me an email with the ' improvements; do it! If you want to send me a letter, write to: ' ' Hermann Seegert ' Leopoldstraáe 36 ' 32756 Detmold ' Germany ' 'Well, I've remarked the procedure as well as I can. I hope, that ' also programming beginners are able to follow my steps. ' Thank you for your attention. ' $lib all off defint a- z %false=0 %true=not %false cls text$="HERMANN Seegert, Hermann Seegert hermann Seegert" 'original string ' searchstring$="hermann" replacestring$="Willfried" ' ? "Original text: " ? text$ ? "Searched string is 'searchstring$' : "; ? searchstring$ 'this string shall be replaced... ? "String instead is 'replacestring$' : "; ? replacestring$ '... with this string ? string$(50,196) 'call the procedure to manipulate the original string.... call replaced(text$,searchstring$,replacestring$,textneu$) ' ? textneu$ ? string$(50,196) ? repeat$(10," FIN ") end ' '---------- REPLACE- Procedure (better than the PB- task!) --------- ' sub replaced(text$,searchstring$,replacestring$,textneu$) searchstring$=ucase$(searchstring$) 'important to set it UCASE ! sl=len(searchstring$) 'the length of the string, which has to be 'replaced redim behind(18000) 'enough for 16 kb length and even more ..... ' ' it's starting.... 'Into the next FOR- NEXT loop the procedure finds out, where the 'next point behind the searchstring is and saves these points into 'the integerarray called 'behind()'. ' for a=1 to tally(ucase$(text$),searchstring$) behind(a)=instr(behind(a-1)+1,ucase$(text$),searchstring$)+sl print a;". position BEHIND 'searchstring$' ";behind(a) next a print string$(50,196) 'only for screen outfit, you can drop that. ' '--- beginning piece .... ' 'In the next part there is the first part of the manipulated 'string. ' part1$=left$(text$,behind(1)-1-sl)+replacestring$ ' '--- center piece .... ' 'This may be is the most complicated part of the whole procedure. 'Here I'm jumping from point to point putting in the 'replace- ' string$'. This part has taken a lot of programming time. ' for a=1 to a-2 part2$=part2$+mid$(text$,behind(a),behind(a+1)-behind(a)-sl)+_ replacestring$ next a ' '--- the endpiece .... ' 'Well, now I'm looking for the rest and put it to the string. The 'new string is ready! If there is no 'endpiece', this doesn't mat- 'ter. ' textneu$=part1$+part2$+mid$(text$,behind(a)) 'that's the whole new string end sub ' '****************************** EOF ********************************