HyperMath

Subranging

Subranging

Previous topic Next topic No expanding text in this topic  

Subranging

Previous topic Next topic JavaScript is required for expanding text JavaScript is required for the print function  

StrSubrange(s, i [, j])

or

string.sub(s, i [, j])

Returns a substring of the string passed.  The substring starts at i.  If the third argument j is not given, the substring ends at the end of the string.  If the third argument is given, the substring ends at and includes the element j.

> print(StrSubrange("Hello HyperMath user", 7))  // from character 7 until the end

HyperMath user

> print(StrSubrange("Hello HyperMath user", 7, 11)) // from character 7 until and including 11

Hyper

> print(StrSubrange("Hello HyperMath user", -10))  // 10 from the end until the end

rMath user

> print(StrSubrange("Hello HyperMath user", -10, -5)) // 10 from the end until 5 from the end

rMath

This can be useful for finding patterns.  For example:

> a,b = StrFind("Hello HyperMath user", "Hy.*")

> s = string.sub("Hello HyperMath user", a,b+1) // excludes last element

> print(s)

HyperMath user