Author Topic: Function: StrReplace  (Read 4226 times)

0 Members and 1 Guest are viewing this topic.

Offline Qbee

  • Newbie
  • Posts: 27
    • View Profile
Function: StrReplace
« on: December 15, 2019, 11:51:02 pm »
I made this function, because it seemed it's functionality is needed and there is no entry in the wiki.
Hope it will be useful.

Code: QB64: [Select]
  1. _TITLE "StrReplace"
  2. ' done 2019-12-16
  3. ' by Myself (Karsten Maske)
  4. '
  5. ' StrReplace$ replaces oldChars by newChars in sourceString
  6. '
  7. ' SYNTAX:
  8. ' newString = StrReplace(oldChars, newChars, sourceString)
  9. '
  10. ' sourceString: string to change
  11. ' oldChars: 0 to length of sourceString; longer string returns sourceString
  12. ' newChars: 0 to whatever fits
  13. '
  14. ' Examples:
  15. '
  16. a$ = "Hello world!"
  17. b$ = StrReplace$("e", "a", a$)
  18.  
  19. a$ = "The quick brown fox jumps over the lazy dog."
  20. b$ = StrReplace$("fox", "duck", a$)
  21.  
  22. b$ = StrReplace$(" ", "", a$)
  23.  
  24.  
  25. FUNCTION StrReplace$ (oldChars AS STRING, newChars AS STRING, sourceString AS STRING)
  26.     DIM o AS INTEGER, n AS INTEGER, i AS INTEGER
  27.  
  28.     n = LEN(sourceString)
  29.     o = LEN(oldChars)
  30.     IF n < 1 THEN
  31.         StrReplace$ = ""
  32.         EXIT FUNCTION
  33.     END IF
  34.  
  35.     IF o > n THEN
  36.         StrReplace$ = sourceString
  37.         EXIT FUNCTION
  38.     END IF
  39.  
  40.     IF n = o THEN
  41.         IF sourceString = oldChars THEN
  42.             StrReplace$ = newChars
  43.             EXIT FUNCTION
  44.         END IF
  45.     END IF
  46.  
  47.     StrReplace$ = ""
  48.     FOR i = 1 TO n
  49.         IF i + o <= n THEN
  50.             IF MID$(sourceString, i, o) = oldChars THEN
  51.                 StrReplace$ = StrReplace$ + newChars
  52.                 i = i + o - 1
  53.             ELSE
  54.                 StrReplace$ = StrReplace$ + MID$(sourceString, i, 1)
  55.             END IF
  56.         ELSE
  57.             StrReplace$ = StrReplace$ + MID$(sourceString, i)
  58.             EXIT FUNCTION
  59.         END IF
  60.     NEXT
  61.  
  62.  

Offline commandvom

  • Newbie
  • Posts: 10
    • View Profile
Re: Function: StrReplace
« Reply #1 on: December 18, 2019, 09:01:11 am »
great!

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Function: StrReplace
« Reply #2 on: December 28, 2019, 03:37:52 pm »
Thank you, it is very usefull for me! Somewhere is very small bug - last character in source string is not replaced. My solution? I add one more space to end the source string and then it work very well!

Thanks!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Function: StrReplace
« Reply #3 on: December 28, 2019, 09:41:31 pm »
Hi, I think there is something simpler:
Code: QB64: [Select]
  1.  
  2. _TITLE "strReplace$ test" 'b+ 2019-12-28
  3. PRINT strReplace$("hello all", "l", "llllll")
  4.  
  5. FUNCTION strReplace$ (s$, replace$, new$) 'case sensitive
  6.     strReplace$ = s$
  7.     p = INSTR(s$, replace$)
  8.     WHILE p
  9.         strReplace$ = MID$(strReplace$, 1, p - 1) + new$ + MID$(strReplace$, p + LEN(replace$))
  10.         p = INSTR(p + LEN(new$), strReplace$, replace$)
  11.     WEND
  12.  
« Last Edit: December 28, 2019, 09:43:26 pm by bplus »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Function: StrReplace
« Reply #4 on: December 31, 2019, 08:20:12 pm »
I actually do this type of work extensively in some html replacement projects. To make it easier, I just feed in the entire document to the program, as a single string then parse the crap out of it until all the replacements are made. Here is the simplest of the methods I have used. It's case sensitive, but this cut-down version will not allow for non-replacement actions on larger words, which contain the search word. Most of the code is actually here just to display it nicely to your screen. The function to replace is only 5-lines.

Pete

Code: QB64: [Select]
  1. WIDTH 80, 25
  2. lmargin = 10
  3. rmargin = _WIDTH - 10
  4.  
  5. a$ = "Steve is a great coder who can accomplish some pretty amazing feats, and some even think of Steve as a god. Well, Steve really isn't a god, he's much better. You see a god has to waste too much of his time on mortals, while Steve can use all his time to create amazing Steve creations that are totally Steveriffic!"
  6.  
  7. PRINT: GOSUB printit: PRINT: _DELAY 5
  8.  
  9. srch$ = "Steve"
  10. replace$ = "Pete"
  11. DO WHILE INSTR(a$, srch$)
  12.     a$ = MID$(a$, 1, INSTR(a$, srch$) - 1) + replace$ + MID$(a$, INSTR(a$, srch$) + LEN(srch$))
  13.  
  14. GOSUB printit
  15.  
  16.  
  17. printit:
  18. IF RIGHT$(a$, 1) <> " " THEN a$ = a$ + " "
  19. seed = 1
  20. DO UNTIL INSTR(seed, a$, " ") = 0
  21.     z$ = MID$(a$, seed, rmargin - lmargin + 2)
  22.     IF lmargin = rmargin THEN z$ = MID$(z$, 1, LEN(z$) - 1)
  23.     IF RIGHT$(z$, 1) = " " THEN
  24.         x = LEN(z$)
  25.     ELSE
  26.         x = _INSTRREV(z$, " ")
  27.     END IF
  28.     IF x = 0 THEN x = rmargin - lmargin + 1
  29.     LOCATE , lmargin
  30.     PRINT RTRIM$(MID$(a$, seed, x))
  31.     seed = seed + x
  32.  

Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/