Author Topic: Split and join strings  (Read 10748 times)

0 Members and 1 Guest are viewing this topic.

Offline codeguy

  • Forum Regular
  • Posts: 174
    • View Profile
Re: Split and join strings
« Reply #15 on: February 17, 2019, 05:02:34 pm »
I use my recursive StringReplace().
It skips things inside quotes or whatever literal marking delimiters you choose. It may not be as fast, namely due to the required string splitting, but it does  the job correctly. Nice work though.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Split and join strings
« Reply #16 on: February 20, 2019, 10:30:20 pm »
I put in three split subs, a diminishing string and a progressive instr() technique, one with exit on length, and one with exit on instr() = 0.

I kept luke's in for comparison. What I like about luke's method is it handles null strings without additional conditions. Nice job, there.  I would have to modify mine at the for/next  printing loop, so it printed one less array, the last one always being null, to get rid of the null condition in my join1 function. 

Oh, I also differ from some in that I don't like using the zero bound in an array, so mine is set up to start words at array element number one, not zero; and yes, I know I could have used OPTION BASE 1.

Code: QB64: [Select]
  1. WIDTH 80, 42
  2.  
  3. REDIM words$(0)
  4.  
  5. original$ = "   The rain   in Spain  "
  6.  
  7. PRINT "Original string: "; original$
  8.  
  9. '------------------------------luke's method.
  10.  
  11. split1 original$, " ", words$()
  12.  
  13. PRINT "Words with luke's method:"
  14. FOR i = LBOUND(words$) TO UBOUND(words$)
  15.     PRINT words$(i)
  16. PRINT "Joined with commas: "; join0$(words$(), ","): PRINT "------------------------------------------------"
  17.  
  18. '------------------------------Pete's dimishing string method.
  19. original$ = "   The rain   in Spain  "
  20.  
  21. split2 original$, " ", words$()
  22.  
  23. PRINT "Words with Pete's dimishing string method:"
  24. FOR i = 1 TO UBOUND(words$)
  25.     PRINT words$(i)
  26. PRINT "Joined with commas: "; join1$(words$(), ","): PRINT "------------------------------------------------"
  27.  
  28. '------------------------------Pete's progressive instr() method A.
  29. original$ = "   The rain   in Spain  "
  30.  
  31. split3 original$, " ", words$()
  32.  
  33. PRINT "Words with Pete's progressive instr() method A:"
  34. FOR i = 1 TO UBOUND(words$)
  35.     PRINT words$(i)
  36. PRINT "Joined with commas: "; join1$(words$(), ","): PRINT "------------------------------------------------"
  37.  
  38. '------------------------------Pete's progressive instr() method B.
  39. original$ = "   The rain   in Spain  "
  40.  
  41. split4 original$, " ", words$()
  42.  
  43. PRINT "Words with Pete's progressive instr() method B:"
  44. FOR i = 1 TO UBOUND(words$)
  45.     PRINT words$(i)
  46. PRINT "Joined with commas: "; join1$(words$(), ","): PRINT "------------------------------------------------"
  47.  
  48. SUB split1 (in$, delimiter$, result$())
  49. REDIM result$(-1)
  50. start = 1
  51.     WHILE MID$(in$, start, 1) = delimiter$
  52.         start = start + 1
  53.         IF start > LEN(in$) THEN EXIT SUB
  54.     WEND
  55.     finish = INSTR(start, in$, delimiter$)
  56.     IF finish = 0 THEN finish = LEN(in$) + 1
  57.     REDIM _PRESERVE result$(0 TO UBOUND(result$) + 1)
  58.     result$(UBOUND(result$)) = MID$(in$, start, finish - start)
  59.     start = finish + 1
  60. LOOP WHILE start <= LEN(in$)
  61.  
  62. SUB split2 (in$, delimiter$, result$())
  63. REDIM result$(LEN(in$) \ 2 + 1)
  64. in$ = in$ + delimiter$
  65.     ii = ii + 1
  66.     DO UNTIL LEFT$(in$, 1) <> delimiter$
  67.         in$ = MID$(in$, 2)
  68.     LOOP
  69.     result$(ii) = MID$(in$, 1, INSTR(in$, delimiter$) - 1)
  70.     in$ = LTRIM$(MID$(in$, LEN(result$(ii)) + 1))
  71. LOOP UNTIL in$ = ""
  72. REDIM _PRESERVE result$(ii)
  73.  
  74. SUB split3 (in$, delimiter$, result$())
  75. REDIM result$(LEN(in$) \ 2 + 1)
  76. in$ = in$ + delimiter$
  77. slen = LEN(in$)
  78.     ii = ii + 1
  79.     DO
  80.         seed = seed + 1
  81.     LOOP UNTIL MID$(in$, seed, 1) <> delimiter$
  82.     IF seed >= slen THEN EXIT DO
  83.     result$(ii) = MID$(in$, seed, INSTR(seed, in$, delimiter$) - seed)
  84.     seed = seed + LEN(result$(ii))
  85. REDIM _PRESERVE result$(ii - 1)
  86.  
  87. SUB split4 (in$, delimiter$, result$())
  88. REDIM result$(LEN(in$) \ 2 + 1)
  89. in$ = in$ + delimiter$
  90.     ii = ii + 1
  91.     DO
  92.         seed = seed + 1
  93.     LOOP UNTIL MID$(in$, seed, 1) <> delimiter$
  94.     IF INSTR(seed, in$, delimiter$) = 0 THEN EXIT DO
  95.     result$(ii) = MID$(in$, seed, INSTR(seed, in$, delimiter$) - seed)
  96.     seed = seed + LEN(result$(ii))
  97. REDIM _PRESERVE result$(ii - 1)
  98.  
  99. FUNCTION join0$ (in$(), delimiter$)
  100. result$ = in$(LBOUND(in$))
  101. FOR i = LBOUND(in$) + 1 TO UBOUND(in$)
  102.     result$ = result$ + delimiter$ + in$(i)
  103. join0$ = result$
  104.  
  105. FUNCTION join1$ (in$(), delimiter$)
  106. IF UBOUND(in$) > 0 THEN
  107.     result$ = in$(1)
  108.     FOR ii = 2 TO UBOUND(in$)
  109.         result$ = result$ + delimiter$ + in$(ii)
  110.     NEXT
  111. join1$ = result$
  112.  
« Last Edit: February 21, 2019, 01:55:29 pm by Pete »
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/