Menu

#48 power for strings!

closed
nobody
None
5
2016-08-04
2016-08-02
No

Hello,

I propose some improvements below that will bring added value to BASIC-256.
They also do not change the normal behavior, but add power to string statements.

LEFT(string, len)

len - number of characters to return. If negative number is given, then the number of characters are removed and the result is returned
Eg.
LEFT("abcdefg", 3) - returns "abc" - translate: Return 3 characters starting from left
LEFT("abcdefg", -3) - returns "defg" - translate: Remove 3 characters starting from left

Cool, logically and useful, right?
print LEFT(s, -3) is equivalent to:
if(length(s)>3) then print right(s, length(s)-3) else print ""
or
print mid(s,4,length(s))

RIGHT(string, len)

len - number of characters to return. If negative number is given,then the number of characters are removed and the result is returned
Eg.
RIGHT("abcdefg", 3) - returns "efg" - translate: Return 3 characters starting from right
RIGHT("abcdefg", -3) - returns "abcd" - translate: Remove 3 characters starting from right

MID(string, pos, len)

len - number of characters to return. If negative number is given, then the number of characters are removed and the result is returned.

print mid("Hello cruel world!", 7, 6)   # result: "cruel "
print mid("Hello cruel world!", 7, -6)  # result: "Hello world!"

pos - position. String indices begin at 1. If negative pos is given,then position is starting from the end of the string, where -1 is the last character position, -2 the second character from the end... and so on

print mid("Hello cruel world!", -6, 5)  # result: "world"
print mid("Hello world!", -6, 5)        # result: "world"
print mid("This is my world!", -6, 5)   # result: "world"

MIDX ( haystack_expr , regex_expr , pos )

INSTRX ( haystack_expr , regex_expr , pos )

pos - position. String indices begin at 1. If negative pos is given,then position is starting from the end of the string, where -1 is the last character position, -2 the second character from the end... and so on

Proposed behavior is unitary (apply in the same way to all similar functions):
1) negative length value removes indicated selection.
3 = return 3 characters at X position
-3 = remove 3 characters at X position
2) negative position starts from the end of string
1 = position of first character from left/start
-1 = position of first character from right/end
Negative position can be useful also for languages that are written from right to left (Hebrew or Arabic languages).

I hope you like what I proposed.
I'm very excited. All old codes will run without errors and new programs will benefit from smarter, intuitive and more powerful functions.

Respectfully,
Florin Oprea

1 Attachments

Discussion

  • Florin Oprea

    Florin Oprea - 2016-08-02

    In this case we no longer need this error message:

    case ERROR_STRNEGLEN:
    errormessage = tr("Substring length less that zero");
    

    And this one should be like this:

    case ERROR_STRSTART:
    errormessage = tr("Starting position cannot be zero");
    

    I forgot to mention

    instr ( haystack_expr , needle_expr , pos , boolean_expr)

    This is the complete version of Interpreter.cpp

     

    Last edit: Florin Oprea 2016-08-02
  • Jim Reneau

    Jim Reneau - 2016-08-04
    • status: open --> closed
     
  • Jim Reneau

    Jim Reneau - 2016-08-04

    code added to 1.99.99.54

     

Log in to post a comment.