|
Post by joreveur on Jul 10, 2017 9:04:18 GMT 1
Hello I look for the equivalent of functions TRIM LTRIM and RTRIM. : trimThank you if see can help me Cordially
|
|
|
Post by vovchik on Jul 10, 2017 11:30:01 GMT 1
Dear joreveur,
You can probably do anything you want as regards trimming strings with CHOP$, which takes useful arguments. However, James C. Fuller did provide some functions way back in 2009, I think, that do what you are looking for (QB style):
' ********************* ' LTRIM$ ' ********************* ' multiple entries in match$ are treated individually
' ------------------ FUNCTION LTRIM$(STRING main$, STRING match$) ' ------------------ LOCAL mlen, mtlen, i, j TYPE int i = 0 j = 0 mlen = LEN(main$) mtlen = LEN(match$) IF (mlen = 0) OR (mtlen = 0) THEN RETURN main$ END IF FOR i = 1 TO mlen IF INSTR(match$, MID$(main$, i, 1), 1) = 0 THEN RETURN RIGHT$(main$, mlen - j) ELSE INCR j END IF NEXT i RETURN main$ END FUNCTION
' -------- ' Example: PRINT LTRIM$(" James Fuller", CONCAT$(" ",CHR$(9))) ' --------
' ********************* ' RTRIM$ ' ********************* ' multiple entries in match$ are treated individually
' ------------------ FUNCTION RTRIM$(STRING main$, STRING match$) ' ------------------ LOCAL mlen, mtlen, i, j TYPE int i = 0 mlen = LEN(main$) j = mlen mtlen = LEN(match$) IF (mlen = 0) OR (mtlen = 0) THEN RETURN main$ END IF FOR i = mlen TO 1 STEP -1 IF INSTR(match$, MID$(main$, i, 1), 1) = 0 THEN RETURN LEFT$(main$, j) ELSE DECR j END IF NEXT i RETURN main$ END FUNCTION
' -------- ' Example: PRINT RTRIM$("James Fuller ", CONCAT$(" ", CHR$(9))); PRINT ' PRINT "More" ' --------
' ********************* ' TRIM$ ' *********************
' ------------------ FUNCTION TRIM$(STRING main$, STRING match$) ' ------------------ LOCAL mlen, mtlen TYPE int LOCAL S1$ TYPE STRING mlen = LEN(match$) mtlen = LEN(match$) IF (mlen = 0) OR (mtlen = 0) THEN RETURN match$ END IF S1$ = LTRIM$(main$, match$) S1$ = RTRIM$(S1$, match$) RETURN S1$ END FUNCTION
' -------- ' Example: PRINT "ABC", TRIM$(" James ", CONCAT$(" ", CHR$(9))), "EFG" FORMAT "%s%s%s\n" ' --------
' ********************* ' EXTRACTSTR$ ' *********************
' ------------------ FUNCTION EXTRACTSTR$(STRING main$, STRING delimiter$) ' ------------------ LOCAL i TYPE int LOCAL sret$ TYPE STRING sret$ = "" IF (LEN(main$) = 0) OR (LEN(delimiter$) = 0) THEN RETURN sret$ END IF i = INSTR(main$, delimiter$, 1) IF i = 0 THEN RETURN main$ END IF RETURN LEFT$(main$, i - 1) END FUNCTION
' -------- ' Example: ' EXTRACTSTR$(main$, delimiter$) ' Similar to LEFT$ but uses a delimiter rather the position S1a$ = EXTRACTSTR$("James C. Fuller"," ") PRINT S1a$ ' --------
' ********************* ' REMAINSTR$ ' *********************
' ------------------ FUNCTION REMAINSTR$(STRING main$, STRING delimiter$) ' ------------------ LOCAL i, j, k, mlen, dlen TYPE int LOCAL sret$ TYPE STRING i = 0 j = 0 k = 0 mlen = LEN(main$) dlen = LEN(delimiter$) IF mlen = 0 THEN RETURN sret$ END IF IF dlen = 0 THEN RETURN sret$ END IF i = INSTR(main$, delimiter$, 1) IF i = 0 THEN RETURN main$ END IF j = i k = i + LEN(delimiter$) WHILE TRUE DO i = INSTR(main$, delimiter$, k) IF i = 0 THEN sret$ = MID$(main$, k, mlen) RETURN sret$ ELSE k = i + LEN(delimiter$) END IF WEND END FUNCTION
' -------- ' Example: S10$ = REMAINSTR$("James C. Fuller", " ") PRINT S10$ ' --------
' ********************* ' TOTAL ' *********************
' ------------------ FUNCTION TOTAL(STRING main$, STRING match$) ' ------------------ LOCAL i, j, k, mlen, matchlen TYPE int mlen = LEN(main$) matchlen = LEN(match$) i = 1 j = 0 k = 0 IF (mlen = 0) OR (matchlen = 0) THEN RETURN j END IF WHILE TRUE DO IF EQUAL(MID$(main$, i, matchlen), match$) THEN INCR j END IF INCR i, matchlen IF i > mlen THEN BREAK END IF WEND RETURN j END FUNCTION
' -------- ' Example: PRINT TOTAL("one,two,three,,four,five,six", ",") ' --------
And analogous functions using CHOP$ are:
' ------------------ FUNCTION LTRIM$(STRING main$, STRING match$) ' ------------------ RETURN CHOP$(main$, match$, 1) END FUNCTION
' ------------------ FUNCTION RTRIM$(STRING main$, STRING match$) ' ------------------ RETURN CHOP$(main$, match$, 2) END FUNCTION
' ------------------ FUNCTION TRIM$(STRING main$, STRING match$) ' ------------------ RETURN CHOP$(main$, match$, 0) END FUNCTION
With kind regards, vovchik
|
|
|
Post by joreveur on Jul 13, 2017 7:54:16 GMT 1
Hello vovchik Thanks you very much for your responses. I checked your functions I have a qustion withe TRIM PRINT "ABC", TRIM$(" James is a boy ", CONCAT$(" ", CHR$(9))), "EFG" FORMAT "%s%s%s\n" and the return is ABCJames is a boyEFG there are more spaces between words. is it a error ? Else do you have a another function to suppress all spaces (or other characters) ? Example PRINT SUPR$("ABCJames is a boyEFG) :' result will be ABCJamesisaboyEFG With my best regards
|
|
|
Post by Pjot on Jul 13, 2017 17:49:27 GMT 1
Else do you have a another function to suppress all spaces (or other characters) ? Example PRINT SUPR$("ABCJames is a boyEFG) :' result will be ABCJamesisaboyEFG With my best regards Hi joreveur, I think what you're looking for can be achieved with EXTRACT$: PRINT EXTRACT$("ABCJames is a boyEFG", " ")
If there are other types of white spaces to be deleted (like TAB or newline), then you can make use of the Regular Expression capability of EXTRACT$: PRINT EXTRACT$("ABCJames is a boyEFG", "[[:space:]]", TRUE)
Please consult 'man 7 regex' where you can find information about the predefined categories to use in situations like these: Hope this helps, Peter
|
|
|
Post by vovchik on Jul 13, 2017 18:48:33 GMT 1
Dear Peter and joreveur,
Thanks... and here is a little example:
' ------------------ FUNCTION PURGE$(STRING txt$, int filter) ' ------------------ LOCAL f$ TYPE STRING SELECT filter CASE 0 f$ = "alnum" CASE 1 f$ = "alpha" CASE 2 f$ = "blank" CASE 3 f$ = "cntrl" CASE 4 f$ = "digit" CASE 5 f$ = "graph" CASE 6 f$ = "lower" CASE 7 f$ = "print" CASE 8 f$ = "punct" CASE 9 f$ = "space" CASE 10 f$ = "upper" CASE 11 f$ = "xdigit" DEFAULT f$ = "space" END SELECT f$ = "[[:" & f$ & ":]]" RETURN EXTRACT$(txt$, f$, TRUE) & " (" & f$ & ")" END FUNCTION
FOR i = 0 TO 11 PRINT i, ". ", PURGE$("ABCJames ? x34 is a boyEFG!", i) NEXT i
With kind regards, vovchik
PS. Mod the RETURN to RETURN EXTRACT$(txt$, f$, TRUE) for PURGE$ to be useful in a program.
|
|