OK, next string question

Discussion in 'Questions (Windows Mobile)' started by Stellaferox, Jun 7, 2007.

  1. Stellaferox

    Stellaferox Active Member Licensed User

    Hi, again a string question. I am working on a datastring replacing and deleting characters which I dont want but the StrRemove and StrReplace function doesnt work in the following example

    D$ = "1, 0" & CRLF & "2, 0" & "3, 0) & CRLF 'spaces after comma intended

    For i = 0 to StrLength(D$)-1
    k = StrAt(D$,i)
    IF k = " " then 'also ASC(k) = 32 and k = chr(32) dont work
    StrRemove(D$," ", 1)
    END IF
    NEXT

    (The reason I am using this FOR next loop is that it also checks for TABS and commas but this code is not included here)
    Now the program passes the IF then condition but doesnt remove (or replace the space with another character). Is this due to the FOR NEXT condition in which the string function is stored and if yes how do I edit a string in a function in which the string itself is examined? Or could it be that a DO WHILE {as long as there are spaces in this string -> remove them} function works better, and if yes how do I state the condition to be fulfilled?
    I can imagine that after editing, the D$ and i-counter isnt updated in the FOR NEXT loop.
    thnx again
    Marc
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    All the string keywords return a new string.
    You should use:
    D$ = StrRemove(D$...)
     
  3. Stellaferox

    Stellaferox Active Member Licensed User

    Erel, OK, I understand, but then I am getting an IndexOutOfRangeException with the k = StrAt(D$,i) statement... the i-counter CAN go past the string if it is shortened truncated in the loop, yes?
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    No. You can't get a character that doesn't exist.
    Just add i = i - 1 after the StrRemove.
     
  5. Stellaferox

    Stellaferox Active Member Licensed User

    hmmmm..... error still exist....
     
  6. petrbury

    petrbury Member Licensed User

    Hi, I don't thing that you can solve the problem by decrementing i, because the end condition for " For - Next " loop remains still the same. I thing, you should use strReplace(D$," ","") for removing spaces ( if I understand correct what do you want ).
    Petr
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    Petr you are right :sign0161:.
    Still it is important to decrease i, otherwise it will skip some characters.
    The solution is to use a While loop.
    Do While i < StrLength(D$)
    ...
    i = i + 1
    Loop
     
  8. petrbury

    petrbury Member Licensed User

    Yes, and maybe it could be done by the other way :

    If strIndexOf(D$," ",0) <> -1 then D$ = strReplace(D$," ","")

    And you don't need to use any loop.

    Petr
     
  9. Stellaferox

    Stellaferox Active Member Licensed User

    Gents,

    I will try this. It looks good!
    thnx for the input!
    Marc
     
  10. Stellaferox

    Stellaferox Active Member Licensed User

    Works!!!
     
Loading...