The art of Strings in oScript

The art of Strings in oScript

Lets have some fun with oScript strings.

Pythonesque feature

String msg = "What a nice and lovely morning!"
msg[1:4] -> "What"        

This is more or less the standard Sub-Stringing, In Position 1 we habe a "W" and up to the position 4, this forms the word "What"

msg[-8,-2] ->"morning"        

Now we have negative numbers. Weird, isn't it?

This is more or less a construnction known from Python.

From the end of the string, we have at the position of the 8th last char a "m", and at the position of the 2nd last char a "g"m The string in between gives "morning"

OScript 1-indexes sequential storage types, such as strings. a[1] refers to the first element of a, a[2] the second, and so on. Negative indices refer to locations starting from the end of the sequence: a[-1] is the final element, a[-2] the second last, and so on. a[0] is always invalid.

One can obtain a subsequence of values by using slice notation: a[n:m] will return the subsequence a[n], a[n+1], a[n+2], …, a[m]. One can use negative indices in a slice, e.g.

 "Hello complete world"[2:-2] will evaluate to "ello complete worl".        

Omitting one end of the slice will extend the slice to either the beginning or end of the sequence: a[:n] returns the first $n$ elements of a, while a[n:] returns the nth element onward.

Continuation

string s = "This is Reiner's nice string" \
                 ' but sometimes its very long'        

This string spans over two lines. The whole string is "This is Reiner's nice string but sometimes its very long" The first line is delimited by "", the continuation char used to be \ (its a recommendation, not a requirement) and the secong line is deliited by '.

Why?

See the Apostroph in "Reiner's". And therefore you have to delimit the first part of the string with " (not '). You can switch ´the demiters on eacfh new string line.

Adding and Subtracting strings

In addition to numerical values, strings can also be added and subtracted from each other. Adding two strings merely concatenates them:

String myString = 'This is a dumb test.'
Echo( myString + ' literally' )                             //displays 'This is a dumb test. Literally'        

Subtraction, on the other hand, removes the first instance of the right operand from the left. If the right string is not a substring of the left, the left string is returned unchanged.

String myString = "This is a nasty test."
Echo( myString - "is" )                              //displays "Th is a nasty test."
Echo( myString - "is" - "is" )                     //displays "Th a nasty test."        

Comparison and Equality

Equality and inequality comparisons between most types are straight-forward: numeric types compare numerically, strings and other container types compare recursively by value.

Comparison operators (e.g. less than, greater than) behave as expected on numerical types. For strings, they perform a case-sensitive lexical ordering, while lists will compare each element in turn until they find one that compares greater or less than the other.

While comparing values of two different types, the result is undefined.

If you compare values of two different types, and a coercion is not possible it just compares the class numbers of the two types (as returned by the Type() function) instead.

To view or add a comment, sign in

More articles by Reiner Merz

Insights from the community

Others also viewed

Explore topics