Results 1 to 18 of 18

Thread: String Functions??

  1. #1

    Thread Starter
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Resolved String Functions??

    Simple problem: Two text boxes, conveniently labled txtText and txtCode. Two command buttons, labled cmdEncode and cmdDecode.

    Take a guess what I need. I don't konw enough about string functions, but I need it to go through the string and replace "a" with "1-", "b" with "2-", and so on and so forth. Shouldn't be hard, but I haven't programmed in months and I'm a little rusty..
    Last edited by timeshifter; Oct 27th, 2004 at 11:56 AM.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    Isn't there a Replace function?
    My usual boring signature: Nothing

  3. #3
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    I'm a little rusty..
    Im a little Chris.



    Replace(stringofdata, data to replace, what to replace it with)
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  4. #4

    Thread Starter
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465
    I'm assuming your "data to replace" and "data to replace with" are both strings in quotes?

  5. #5
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    yes
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  6. #6
    Lively Member Xcoder's Avatar
    Join Date
    Jan 2004
    Posts
    120
    youre right, or a string variable.
    Last edited by Xcoder : 09-10-2001 at 12:45 AM.

  7. #7

    Thread Starter
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465
    Too many variables in the replace function.. what do they all mean?

  8. #8
    Lively Member
    Join Date
    Sep 2004
    Posts
    96
    Here's how I undid it.....
    VB Code:
    1. Private Sub cmdDecode_Click()
    2. Dim myCode As String
    3. Dim CodeChar As String
    4. Dim intCount As Integer
    5. Dim Needle As String
    6. Dim intLetterNum As Integer
    7.  
    8.     myCode = txtCode.Text
    9.    
    10.     intLetterNum = Asc("Z")
    11.    
    12.     For intCount = 26 To 1 Step -1
    13.         CodeChar = Chr$(intLetterNum) 'This is what the "code" will be (letters Z through A)
    14.         Needle = CStr(intCount) & "-" 'Needle is the item being looked for... a number followed by a dash.
    15.        
    16.         myCode = Replace(myCode, Needle, CodeChar) 'In MyCode, replace all ocurrances of Needle with CodeChar
    17.        
    18.         Needle = CStr(intCount) & " " ' Not all numbers were followed by a dash, some had a space.
    19.        
    20.         myCode = Replace(myCode, Needle, CodeChar & " ")
    21.        
    22.         Needle = CStr(intCount) & "." 'While others were followed by a period
    23.        
    24.         myCode = Replace(myCode, Needle, CodeChar & ".")
    25.        
    26.         intLetterNum = intLetterNum - 1 'Back up one letter; Z, y, X.... C, B, A
    27.     Next
    28.    
    29.     txtText.Text = myCode
    30.      
    31. End Sub

    Ok, now for the why I went Z to A instead of the other way around.
    Simple.
    Consider this: 1-14-4-
    1- = A
    14- = N
    4- = D

    If I replace all 1- with "A", I get A14-4-
    If I were to then replace 4- with "D", I'd get A1DD..... which isn't right.
    But in working in reverse:
    IF I replace 14- with "N" I'm left with 1-N4-
    I can then safely (and correctly) replace 4- with "D" to get 1-ND
    And finally replacing 1- with "A" gives me the final AND.

  9. #9

    Thread Starter
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465
    I'm still having problems getting it to actually encode it... There is a replace function, but I dont' know what numbers I need in the command for it to register. It keeps telling me I'm missing something, but I can't figure out where.

  10. #10
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    Run em backwards from highest to lowest

    14-
    4-
    1-

    That way, 14- was already replaced by a char so the error on 4- is avoided with Replace()

  11. #11

    Thread Starter
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465
    I still can't get the Replace() function to work. That's my big question. Once I get that, I can solve the rest of it. The only problem is that I can't figure out the syntax for the function.

  12. #12
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    And unless you provide sample of the raw, encoded and decoded strings, we can only guess what your trying to achieve.

    Someone mentioned spaces and periods were also used.

  13. #13

    Thread Starter
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465
    All I need is the syntax to make Replace() work. When that's done, I'll put up the entire program so you can see how I did it. Syntax is all I need right now, and the VB Index isn't being that helpful.

  14. #14
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    MSDN

    Replace(expression, find, replacewith[, start[, count[, compare]]])

    expression Required.String expression containing substring to replace.
    find Required. Substring being searched for.
    replacewith Required. Replacement substring.
    start Optional. Position within expression where substring search is to begin. If omitted, 1 is assumed.
    count Optional. Number of substring substitutions to perform. If omitted, the default value is –1, which means make all possible substitutions.
    compare Optional. Numeric value indicating the kind of comparison to use when evaluating substrings. See Settings section for values.

    Settings
    vbUseCompareOption or –1 Performs a comparison using the setting of the Option Compare statement.
    vbBinaryCompare or 0 Performs a binary comparison.
    vbTextCompare or 1 Performs a textual comparison.
    vbDatabaseCompare or 2 Microsoft Access only. Performs a comparison based on information in your database.

  15. #15

    Thread Starter
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465
    Now make it fully work without VB telling you that you are missing = somewhere.

  16. #16
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    So what's the problem? Your only going to get errors if you

    - didn't pass the required data type
    - you passed a wrong data type
    - you didn't store the result of Replace, strRet = Replace(), and you didn't use Call

    Your replacing all instances so start and count options are irrelevant and should be left with default values.

    Sample code for usage was already posted.



    I strongly suggest you post your code if your still getting errors.
    Last edited by leinad31; Oct 27th, 2004 at 08:53 AM.

  17. #17
    Lively Member
    Join Date
    Sep 2004
    Posts
    96
    Originally posted by timeshifter
    Now make it fully work without VB telling you that you are missing = somewhere.
    Dude, replace is a function....
    varable = Replace(haystack, needle, replaceWith)
    Simple.... did you not look at the code that I posted? It clearly used the replace function.

    haystack = the variable/string you want to search in
    needle = the variable/string you are looking for
    replaceWith = the variable/string to replace your needle with when needle is found in haystack.

  18. #18

    Thread Starter
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465
    Sorry, brain freeze. Haven't programmed in a while, and I've forgotten some of the stuff. My apologies, Talon.

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