VB6 string trunk

Soldato
Joined
18 Oct 2002
Posts
4,925
got a string like so


12345, 12345, 12345, 12345, 12345

I need to ensure it doesn't grow longer than 8000 characters so I need to tryim it to the nearest ',' on the right under 8000 characters.

Any ideas ?

Oh yes and using VB6
 
String = "Hello World"

stringLength = Len(String)

trimmedString = Mid(String, 1, 5) [1 is the place in the string you want to start, 5 is how many characters from the start you want to choose]

stringLength would return 11

trimmedString would return "Hello"

You can use Trim to get rid of any trailing spaces or ,
 
right not sure if I was totaly clear.

I can trim spaces, and cut down the string to x number of characters etc. My problem is I need it to be cut to a specific character a ',' before the cut off point i.e.

This is the string, but in reality it can be any length as its generated dynamically.

12345, 12345, 12345, 12345, 12345

Now say the cut off is 24 characters, is would make the string

12345, 12345, 12345, 123

But then I need to cut this again so the string is

12345, 12345, 12345,

is there a function in vb6 that will allow me to locate the first instance of a certain character i.e. a ',' which is closest to the right had side of the string ??

The only way I can think to do it would be to cut the string then get the length of the cut string and walk though it a character at a time from the right and remove everything until I encounter a ','.
 
The pattern "12345, " [inc space] repeats 1333 times in 8000 characters [remainder 2]. So you could just cut the string to 7998 characters. Or 7996 if you dont want the last ", "
 
lol i think he means that the string can be anything and 12345 is just an example.

the only way i can think of doing it is using a chars() function. so make a loop:

n = 8000
string = 12345, 12345 etc.

while (string.chars(n) <> ','){
*remove 1 char from the end of string and decrement n*
}

return string

i dunno if this will work as i dont know visual basic and chars is a .net function i think but there must be some equivalent
 
I am guessing from his use of "12345, 12345, 12345" his string is a list of numbers/text that are 5 characters long [be it text, numbers or both] separated by a commar and space.

If the list includes items that are less/greater than 5 characters in length, my idea wont work :p
 
This any use?

Code:
Public Function StringTruncate(ByVal str As String, char As String, maxLength As Long) As String
    If Len(str) > maxLength + Len(char) Then
        str = Left(str, maxLength + Len(char))
    End If
    
    If Right(str, Len(char)) = char Then
        StringTruncate = Left(str, Len(str) - Len(char))
    ElseIf Len(str) < Len(char) Then
        StringTruncate = ""
    Else
        StringTruncate = StringTruncate(Left(str, Len(str) - 1), char, maxLength)
    End If
End Function
 
Last edited:
This any use?

Code:
Public Function StringTruncate(ByVal str As String, char As String, maxLength As Long) As String
    If Len(str) > maxLength + Len(char) Then
        str = Left(str, maxLength + Len(char))
    End If
    
    If Right(str, Len(char)) = char Then
        StringTruncate = Left(str, Len(str) - Len(char))
    ElseIf Len(str) < Len(char) Then
        StringTruncate = ""
    Else
        StringTruncate = StringTruncate(Left(str, Len(str) - 1), char, maxLength)
    End If
End Function

Umm looking at the code I think it might, just realy doing what I mentioned and triming to to length needed then walking 1 char each time from the right till it finds a ','.

Will give it a bash when I get back in.

Thanks mate
 
Gman, could you give us an example of your string?

Because if my assumption is correct [which it probably isn't] my code would be so much easier :p
 
If your string literally is always 5 characters/numbers each time then go with what SiriusB came up with...


Try this though

Create a blank form, add two textboxes and one command button

paste the following code

Code:
Private Sub Command1_Click()
'create a variable called mystr
mystr = Text1.Text


                'split the text
                textsplit = Split(mystr, ",")
                'numcom is popoluated with UBound, giving us the total number of pieces of info in the string
                numcom = UBound(textsplit)
                
'replace 8 with 8000, if our string is larger than 8000 characters then take one comma off
                If Len(mystr) > 8 Then
                numcom = numcom - 1
                End If
                x = 1
                
                txtvar = 1

'do until x = to the number of commas
                Do Until x = numcom
                
'keep adding to our strvar variable until we hit a comma
                    strvar = strvar & Mid(mystr, txtvar, 1)
'increase the amount in the string by 1
                txtvar = txtvar + 1
'if we've ended on a comma, add one onto our count
                If Right(strvar, 1) = "," Then
                x = x + 1
                End If
                Loop

'populate text2 with our new string                               
                Text2.Text = strvar
      
End Sub
 
Last edited:
nice one managed to get both version working although nikebee yours is in .net not VB6 :)

thanks a lot guys top work
 
nice one managed to get both version working although nikebee yours is in .net not VB6 :)

thanks a lot guys top work

it's in VB6 mate... I wrote it in VB6 :)

Code works absolutely fine, I've used versions of it in a number of projects for work related - all compiled and exported in Visual Basic 6.

What makes you think it's .Net?
 
Back
Top Bottom