Results 1 to 8 of 8

Thread: [RESOLVED] obtain from a string

  1. #1

    Thread Starter
    Member nf_vb's Avatar
    Join Date
    Mar 2007
    Location
    Portugal
    Posts
    35

    Resolved [RESOLVED] obtain from a string

    it want to obtain from a string the next result: pri004

    The string:
    C:\Documents and Settings\nf\Desktop\pri004_02052007.xml

    how can i do it....??

    thx
    ----------------------------------------
    never drive faster than your angel can fly
    ----------------------------------------

  2. #2
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: obtain from a string

    You mean get a certain part of a string?

  3. #3
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: obtain from a string

    Is it just this string you want, or do you want to do it with other strings aswell?

    You will probably need to know a few things like, is everything in front of "pri004" always the same, is the length of substring "pri004" always the same etc...

    if you know that you can use things like Mid$(), InStr() etc to get it.


    For example, you could use:
    Code:
    Dim myvar As String
    Dim mystring As String
    Dim StartPos As Integer
    mystring = "C:\Documents and Settings\nf\Desktop\pri004_02052007.xml"
    
    
    StartPos = InStrRev(mystring, "\")
    
    myvar = Mid$(mystring, StartPos + 1, 6)
    StartPos is the position of the last "\" symbol, found using "InStrRev".
    Then the Mid$() fucntion finds the part of the string that starts 1 position after StartPos (StartPos + 1) and is 6 characters long, which in this case is "pri004".
    Last edited by NickThissen; May 2nd, 2007 at 04:17 AM.

  4. #4

    Thread Starter
    Member nf_vb's Avatar
    Join Date
    Mar 2007
    Location
    Portugal
    Posts
    35

    Re: obtain from a string

    yes.

    i only want to obtain pri004 from that string or the content from "pri" to the character "_"
    ----------------------------------------
    never drive faster than your angel can fly
    ----------------------------------------

  5. #5
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: obtain from a string

    vb Code:
    1. Dim str As String
    2. str = "C:\Documents and Settings\nf\Desktop\pri004_02052007.xml"
    3. Text1.Text = Mid$(str, 38, 6)

  6. #6
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: obtain from a string

    This would be another way...
    You copy and paste the path into text1 and then click this button. And if the path contains "pri" then in text2 it will show "pri" & the 3 digits after it.

    vb Code:
    1. Private Sub Command1_Click()
    2. Dim str As String
    3. str = Text1.Text
    4. If InStr(1, str, "pri") > 0 Then
    5. Text2.Text = Mid$(str, 38, 6)
    6. End If
    7. End Sub

  7. #7
    Fanatic Member robbedaya's Avatar
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    872

    Re: obtain from a string

    vb Code:
    1. Private Sub Command1_Click()
    2. Dim str As String
    3. Dim i1 as integer, i2 as integer, i3 as integer
    4. str = Text1.Text
    5.  
    6. i1 = InStr(1, str, "pri") 'location of pri in the string
    7. i2 = InStr(i1, str, "_")  'location of the first _ in the string after "pri"
    8. i3  = i2 - i1 -1 'length of the string you want to get
    9. If i1 > 0 And i2 > 0 And i3 > 0 Then
    10. Text2.Text = Mid$(str, i1, i3)
    11. End If
    12. End Sub

    (Note that this is free-hand coding, it was not tested, it could contain errors, but it should get you on the way)
    - Use the thread tools to Mark your Thread as Resolved when your question is answered.
    - Please Rate my answers if they where helpful.

  8. #8

    Thread Starter
    Member nf_vb's Avatar
    Join Date
    Mar 2007
    Location
    Portugal
    Posts
    35

    Re: obtain from a string

    thx robbedaya...it was that I was needing.
    thx also all who answered me.

    thx community
    ----------------------------------------
    never drive faster than your angel can fly
    ----------------------------------------

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