Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MANIPULATING STRINGS

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
HOW DO YOU DELETE A WORD IN A STRING( AS IN A VARIABLE WITH
THE $)(THE WAY I ADD STRINGS IS CALLED CONCATENATION. (AS IN
WORD = WORD + ",SOMETHING")
BUT WHEN I PUT THE MINUS IN IT TO DELETE A WORD IN A STRING,
IT WON'T LET ME.
 
find your position that you want removed
new variable =left$ to position-1 + right$ or mid$ from postion +1
Ed Fair
unixstuff@juno.com
Any advice I give is my best judgement based on my interpretation of the facts you supply. Help increase my knowledge by providing some feedback, good or bad, on any advice I have given.
 
Play around with this for a while...

A$ = A$ + "Hello."
A$ = A$ + " This is the word I wish to remove 'Text'. Hey! It's gone!"
A$ = RemoveString$(A$, "Text")
PRINT A$

FUNCTION RemoveString$ (RemoveFrom AS STRING, WhatToRemove AS STRING)
FoundIt = 0
RemoveTemp$ = ""
FOR Looper = 1 TO LEN(RemoveFrom) - LEN(WhatToRemove) + 1
IF UCASE$(MID$(RemoveFrom, Looper, LEN(WhatToRemove))) = UCASE$(WhatToRemove) THEN
FoundIt = Looper
END IF
NEXT
IF FoundIt > 0 THEN
IF FoundIt > 1 THEN
LeftMod = 1
RemoveTemp$ = LEFT$(RemoveFrom, FoundIt - LeftMod)
END IF
IF FoundIt + LEN(WhatToRemove) <= LEN(RemoveFrom) THEN
RemoveTemp$ = RemoveTemp$ + RIGHT$(RemoveFrom, LEN(RemoveFrom) - FoundIt - LEN(WhatToRemove) + 1)
END IF
END IF
IF RemoveTemp$ = &quot;&quot; THEN RemoveTemp$ = RemoveFrom
RemoveString$ = RemoveTemp$
END FUNCTION
 
This should work, too:

FUNCTION RemoveString$ (RemoveFrom AS STRING, RemoveText AS STRING)

DIM ltposition AS INTEGER, rtposition AS INTEGER
DIM src AS STRING

src=RemoveFrom
ltposition = INSTR(src, RemoveText)

DO WHILE ltposition > 0
rtposition = LEN(src) - (ltposition + LEN(RemoveText))
src = LEFT$(src, ltposition) + RIGHT$(src, rtposition)
ltposition = INSTR(src, RemoveText)
LOOP

RemoveString$ = Source

END FUNCTION
 
Here's a bug fix for your function...

FUNCTION RemoveString$ (RemoveFrom AS STRING, RemoveText AS STRING)
DIM ltposition AS INTEGER, rtposition AS INTEGER
DIM src AS STRING
src=RemoveFrom
ltposition = INSTR(src, RemoveText) - 1
DO WHILE ltposition > 0
rtposition = LEN(src) - (ltposition + LEN(RemoveText))
src = LEFT$(src, ltposition) + RIGHT$(src, rtposition)
ltposition = INSTR(src, RemoveText)
LOOP
RemoveString$ = Src
END FUNCTION
 
Pretty good... I like it... * HOW DARE YOU SHORTEN MY OVERSIZED FUNCTION YET RETAIN IT'S FUNCTIONALITY! *... :)
 
Sorry about incorporating the bugs in that. Glad to have someone around sharp enough to catch them.
 
Actually, it seems that while Nyaj2k1 fixed a bug, he added one too. The updated version will not remove any of the matching substrings if one of them occurs at the start of the string. The following function should work as desired:
[tt]
FUNCTION RemoveString$(in$, substring$)
src$ = in$
offset% = INSTR(src$, substring$)
DO WHILE offset%
src$ = LEFT$(src$, offset% - 1) + MID$(src$, offset% + LEN(substring$))
offset% = INSTR(src$, substring$)
LOOP
RemoveString$ = src$
END FUNCTION
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top