Nick G
Nick G

Reputation: 1229

How to remove spaces in a string after a certain character?

I'm just wondering if there is a way to replace all blank spaces after a certain character in a string. Basically a string like;

str = "This is a test - 1, 2, 3, 4, 5"

I would like essentially remove all of the spaces after the '-'. I understand how to do the

replace(str," ","") 

but that will remove every space, and I want to keep the 'This is a test -" intact for readability to the user. I have used

Instr(str,"-") 

to get the position of that character but do not know how to then enact the replace function on the rest of the string starting from that point.

Upvotes: 1

Views: 2061

Answers (1)

Bagger
Bagger

Reputation: 363

I would used regex but if you just want to use string functions I think this is what you are asking

str = "This is a test - 1, 2, 3, 4, 5"
chrPos = Instr(str,"-")
lStr = Left(str, chrPos + 1)
rStr = Replace(str , " " , "", chrPos+1)
wscript.echo lStr & rStr

The result is This is a test - 1,2,3,4,5

Upvotes: 2

Related Questions