Results 1 to 2 of 2

Thread: Strings

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Posts
    225
    Hiya,

    How do you seperate text from a string? eg.

    Text Here (92)

    I need to seperate the text 'Text Here' (not the space after 'Here'), and the number in the brackets.

    Thanks.

    -Git

  2. #2
    Fanatic Member
    Join Date
    Apr 2000
    Location
    Whats a location?
    Posts
    516
    There are various ways of doing this:

    Code:
    Dim NewString As String, TheString As String
    Dim Number As Integer
    
    TheString = "Text Here (93)"
    
    'Takes that first 9 chars and puts them into a string
    NewString = Left$(TheString, 9)
    Number = RemoveBrackets(Right$(TheString, 4))
    Code:
    Dim NewString As String, TheString As String
    Dim Number As Integer
    
    TheString = "Text Here (93)"
    
    'Finds the first bracket and takes everything before that
    'and then removes the space
    NewString = Trim$(Left$(TheString, CLng(InStr(1, TheString, "(", vbTextCompare) - 1)))
    'Finds the brackets (doesn't assume that they are at the end)
    Number = RemoveBrackets(Right$(TheString, CLng(InStr(1, TheString, ")", vbTextCompare) - Len(NewString))))

    Code:
    '//REQUIRES VB6
    Dim NewStringArr() As String, TheString As String
    Dim NewString As String
    Dim Number As Integer
    Dim i As Integer
    
    TheString = "Text Here (93)"
    
    NewStringArr = Split(TheString, " ")
    For i = 0 To UBound(NewStringArr)
        If NewStringArr(i) Like "(*)" Then
             Number = RemoveBrackets(NewStringArr(i))
             Exit For
        Else:
             NewString = NewString & NewStringArr(i) & " "
        End If
    Next i
    
    NewString = Trim$(NewString)

    Or

    Code:
    '// REQUIRES VB 6
    Dim NewString As String, TheString As String
    Dim Number As Integer
    TheString = "Text Here (93)"
    'Finds the first bracket and takes everything before that
    'and then removes the space
    NewString = Trim$(Left$(TheString, CLng(InStrRev(TheString, "(", , vbTextCompare) - 1)))
    Number = RemoveBrackets(Right$(TheString, CLng(InStrRev(TheString, ")", , vbTextCompare) - 1) - Len(NewString)))
    The function they all use:
    Code:
    Function RemoveBrackets(BracketString As String) As Integer
    'Remove Any Spaces
    BracketString = Trim$(BracketString)
    '//If you have VB6 Then:
    BracketString = Replace(BracketString, "(", "")
    BracketString = Replace(BracketString, ")", "")
    BracketString = Trim$(BracketString)
    RemoveBrackets = CInt(BracketString)
    '//Otherwise:
    BracketString = Left$(BracketString, 3)
    BracketString = Trim$(BracketString)
    BracketString = Right$(BracketString, 2)
    BracketString = Trim$(BracketString)
    RemoveBrackets = CInt(BracketString)
    End Function
    I hope you find one of these useful,

    Me.

    [Edited by V(ery) Basic on 08-27-2000 at 06:12 AM]
    Courgettes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width