Author Topic: String.Insert  (Read 955 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
String.Insert
« on: June 02, 2020, 10:03:13 am »
I'm sure you all already have all sorts of ways to manipulate strings but I figured I'd add this little function if anyone wants some feeling of the .NET string methods. I borrowed the Replace method from Steve McNeil in another post and then did a light edit to make the Remove function.
Code: QB64: [Select]
  1. PRINT String.Insert("Helloworld", ", you ridiculously stupid ", 6)
  2. PRINT String.Replace("Hello Earth","Earth","World")
  3. PRINT String.Remove("Hello, crazy world",", crazy")
  4.  
  5. FUNCTION String.Insert$ (toChange AS STRING, insert AS STRING, position AS INTEGER)
  6.     toChange = MID$(toChange, 1, position - 1) + insert + MID$(toChange, position, LEN(toChange) - position + 1)
  7.     String.Insert = toChange
  8.  
  9. FUNCTION String.Replace$ (a AS STRING, b AS STRING, c AS STRING)
  10.     j = INSTR(a, b)
  11.     IF j > 0 THEN
  12.         r$ = LEFT$(a, j - 1) + c + String.Replace(RIGHT$(a, LEN(a) - j + 1 - LEN(b)), b, c)
  13.     ELSE
  14.         r$ = a
  15.     END IF
  16.     String.Replace = r$
  17.  
  18. FUNCTION String.Remove$ (a AS STRING, b AS STRING)
  19.     DIM c AS STRING
  20.     c = ""
  21.     j = INSTR(a, b)
  22.     IF j > 0 THEN
  23.         r$ = LEFT$(a, j - 1) + c + String.Remove(RIGHT$(a, LEN(a) - j + 1 - LEN(b)), b)
  24.     ELSE
  25.         r$ = a
  26.     END IF
  27.     String.Remove = r$
« Last Edit: June 02, 2020, 11:22:33 am by SpriggsySpriggs »
Shuwatch!