开发者

VB6 getting ride of large spaces

开发者 https://www.devze.com 2023-01-09 22:27 出处:网络
Hey all, i am trying to replace large spaces between text with just one. My output looks like this right now:

Hey all, i am trying to replace large spaces between text with just one. My output looks like this right now:

5964215">

        This is just the first example of the spaces
    5964478">

        This would be the 2nd example of showing how many spaces this thing has in each sentence.
    5964494">

That comes from a textbox that has multi-line to true. Here is what it looks like when it doesn't have multi-line to true.

http://www.june3rdsoftware.com/forums/vb6.jpg

I can开发者_开发知识库 not seem to get the spaces to go away! BTW, this text is from a webpage if that makes any difference.

David


According to the suggestion of MvanGeest, here is some VB code to replace blocks of white spaces:

Sub test()
    Dim x As String, y As String
    x = "abcd       defg             1233"
    Dim re As New RegExp
    re.Pattern = "\s+"
    re.Global = True
    y = re.Replace(x, " ")
    Debug.Print y
End Sub

To make this work, you will have to add a reference to "Microsoft VBScript Regular Expresssions" to your project.


Assuming no regex support, why not set up a simple state machine that will set the state=1 when a space is found and set state=0 once a non-space is encountered. You can move char by char when state=0 (thus copying over only 1 space per series of spaces).


Also assuming no regex, you could try something like

str = "long       text           with         spaces     "
i = LenB(str)
str = Replace(str, "  ", " ")
Do While LenB(str) <> i
  i = LenB(str)
  str = Replace(str, "  ", " ")
Loop

Of course this code could be optimized for long space sequences but it might be all you need as well

0

精彩评论

暂无评论...
验证码 换一张
取 消