如何删除字符串中某个字符后的空格?
How to remove spaces in a string after a certain character?
我只是想知道是否有办法替换字符串中某个字符后的所有空白 space。基本上是一个字符串;
str = "This is a test - 1, 2, 3, 4, 5"
我想基本上删除“-”之后的所有 space。我知道如何做
replace(str," ","")
但这会删除每个 space,我想保留“这是一个测试 -”,以便用户阅读。我使用了
Instr(str,"-")
获取该字符的位置,但不知道如何对从该点开始的字符串的其余部分执行替换功能。
我会使用正则表达式,但如果你只想使用字符串函数,我想这就是你要问的
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
结果是 This is a test - 1,2,3,4,5
VBScript REPLACE
函数有一个 start 参数,但它没有按您预期的方式工作。因此,您必须隔离要执行替换的部分:
Dim parts
parts = Split("This is a test - 1, 2, 3, 4, 5", "-", 2) ' returns array with 2 items (max)
Debug.Print parts(0) & "-" & Replace(parts(1), " ", "") ' replace and concatenate
我只是想知道是否有办法替换字符串中某个字符后的所有空白 space。基本上是一个字符串;
str = "This is a test - 1, 2, 3, 4, 5"
我想基本上删除“-”之后的所有 space。我知道如何做
replace(str," ","")
但这会删除每个 space,我想保留“这是一个测试 -”,以便用户阅读。我使用了
Instr(str,"-")
获取该字符的位置,但不知道如何对从该点开始的字符串的其余部分执行替换功能。
我会使用正则表达式,但如果你只想使用字符串函数,我想这就是你要问的
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
结果是 This is a test - 1,2,3,4,5
VBScript REPLACE
函数有一个 start 参数,但它没有按您预期的方式工作。因此,您必须隔离要执行替换的部分:
Dim parts
parts = Split("This is a test - 1, 2, 3, 4, 5", "-", 2) ' returns array with 2 items (max)
Debug.Print parts(0) & "-" & Replace(parts(1), " ", "") ' replace and concatenate