Results 1 to 11 of 11

Thread: [RESOLVED] String Manipulation

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2007
    Location
    Riverton, WY
    Posts
    36

    Resolved [RESOLVED] String Manipulation

    VB Code:
    1. If InStr(sockBuff, "PRIVMSG") <> 0 Then
    2.         Select Case True
    3.             Case InStr(sockBuff, "!roll") <> 0:
    4.                 y = Split(Mid(sockBuff, InStr(sockBuff, " : !roll ")), " ")
    5.                 sendToIRC (sockBuff)
    6.              '   status(0) = DCCCHat
    7.             '    sckDCC(sckDCC.UBound).Connect ircGetIP(y(UBound(y) - 1)), Replace(y(UBound(y)), "", "")

    I commented out the the last two lines. I thought they were causing me an error, and I don't want DCC anyway. Where I have "!roll" entered, it used to say "DCC CHAT". It keeps getting an error at the following bit of code:

    VB Code:
    1. y = Split(Mid(sockBuff, InStr(sockBuff, " : !roll ")), " ")

    I hit F2 and typed in SPLIT, but I'm afraid I am not able to figure out what all it's trying to do here. Can anyone explain it to me?




    Also, how would I go about getting the first "CyberInfantry" only out of the following string:

    :[email protected] PRIVMSG #shadowfall :!roll

  2. #2

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2007
    Location
    Riverton, WY
    Posts
    36

    Re: String Manipulation

    Run-time error '5':
    Invalid procedure call or argument

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: String Manipulation

    Well what
    VB Code:
    1. y = Split(Mid(sockBuff, InStr(sockBuff, " : !roll ")), " ")
    says is

    1. InStr asks if the string " : !roll " is in sockBuff. If it finds it InStr will return a number that is the character number where the string was found. If not found it will return 0.
    2. Mid then tries to extract a portion of sockBuff from that character to the end of the string, and
    3. Split then tries to split it up at the spaces.

    The problem is probably happening when the string isn't found by InStr since the zero that would be returned is not valid in the Mid command.

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2007
    Location
    Riverton, WY
    Posts
    36

    Re: String Manipulation

    So the InStr() command would be what I would need in order to find the first "CyberInfantry" in that other question I had?

    Then I would need to find the first !...

    And then do a Right(InStr(sockBuff, "!")), then a Left(sockBuff, Len(sockBuff) - 1) ?

    Would that get me down to only "CyberInfantry" out of:
    :[email protected] PRIVMSG #shadowfall :!roll

    ?

  6. #6
    Frenzied Member
    Join Date
    Aug 2005
    Posts
    1,042

    Re: String Manipulation

    CyberInfantry:

    I’m not real sure what you are doing here, but I don’t think you are using the InStr() function correctly.

    The InStr Function returns a Variant (Long) specifying the position of the first occurrence of one string within another.

    The Syntax is: InStr([start, ]string1, string2, [compare])

    Start is a numeric expression that sets the starting position for each search.

    String1 is the String expression that is being searched.

    String2 is the String expression you are trying to find.

    Compare is a value that indicates the type of comparison.
    The possible values are:
    0 = Performs a binary comparison.
    1 = Performs a textual comparison
    2 = Is used for Microsoft Access only for info in a database.

    An example of using the InStr function to return the position of the first occurrence of one string within another would be like this:

    VB Code:
    1. Dim SearchString, SearchChar, MyPos
    2. SearchString = "XXpXXpXXPXXP"   'The string to search
    3. SearchChar =  "p"           'Search for "p".
    4.  
    5. MyPos = InStr(4, SearchString, SearchChar, 1)
    6.  
    7. 'This would return 6, because the code is saying
    8. 'start at the 4th character in the SearchString, which is X
    9. 'and search for the SearchChar, which is p, and tell
    10. 'me where you find it.  The return value of 6 means
    11. 'that it was found as the 6th character.
    So your code statement: InStr(sockBuff, "PRIVMSG") doesn’t make sense.

    If you want to compare two strings then maybe you should use the StrComp() Function.
    Not sure if this helps you or not.
    Last edited by AIS4U; Jan 22nd, 2007 at 09:34 PM. Reason: correct typos

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: String Manipulation

    VB Code:
    1. Const STRINGTOBEFOUND = "CyberInfantry"
    2. Const TESTDATA = ":[email protected] PRIVMSG #shadowfall :!roll"
    3.  
    4. Dim intStartPos As Integer
    5.  
    6. intStartPos = InStr(TESTDATA, STRINGTOBEFOUND)
    7. If intStartPos > 0 Then
    8.     MsgBox Mid$(TESTDATA, intStartPos, Len(STRINGTOBEFOUND))
    9. End If

  8. #8

    Thread Starter
    Member
    Join Date
    Jan 2007
    Location
    Riverton, WY
    Posts
    36

    Re: String Manipulation

    AISU4:
    I see what you are saying about the InStr() command. That was all ready in the code as it is, though. Is it possible that, if no numerical data is entered, it starts off at the beginning?



    MartinLiss:
    What are the $ signs for? I remember seeing them... only once in my VB class. That had to do with opening files and closing them. What do they mean?

    Also, the string I'm trying to find within the larger string changes.

    This is a bit of code from penderj's VB IRC Bot. IRC will have many different users. I copied that string from my Debug window when I had my text persona in the IRC room say something (trying to figure out the IRC protocols).

    With each person, that name is going to be different. However, I KNOW it's going to start with ":" and I KNOW that after the name, it's going to have a "!".

    Would the code I posted in my last post work for that?

  9. #9

  10. #10
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: String Manipulation

    Quote Originally Posted by CyberInfantry
    AISU4:
    ...Also, the string I'm trying to find within the larger string changes....
    I used constants only for my testing and you would probably use some string variable that contained the word(s) you want to find and another one for the text to be searched instead of the Consts.

  11. #11
    Frenzied Member
    Join Date
    Aug 2005
    Posts
    1,042

    Re: [RESOLVED] String Manipulation

    CyberInfantry:
    if no numerical data is entered, it starts off at the beginning?
    Yes, that is correct. It is an optional argument and if you leave it out it starts from the beginning of the string.

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