#111 Additional string functions


This patchset implements a substring operator, in the
form of a range specifier than may be postfixed to any
string-valued expression. It is pretty straightforward,
but I post it here for comments on the syntax.


substring = string[begin:end]

where begin and end are integer indices specifying
character position in the orginal string. As with the
existing gnuplot range specifiers, these may be empty
or '*' to indicate auto-expansion to one extreme of the

Example output from .../demo/substring.dem

each line is from a print statement of the form
print "foo[x:y] =", foo[x:y]

foo = ABCDEF
foo[2:4] = CDE
foo[0:0] = A
foo[4:2] =
beg = 1 end = 3
foo[beg:end] = BCD
foo[end:beg] =
foo[4:] = EF
foo[4:*] = EF
foo[:] = ABCDEF
foo[*:*] = ABCDEF

Possible issues

The range specifier notation seems appropriate, but it
is exactly equivalent to providing a subroutine
substring(string,begin,end). Does anyone have a
preference for the subroutine form?

The indexing uses C convention; the start of the string
is foo[0:*], not foo[1:*]. This is maybe annoying to
non-C programmers, but would still be an issue if the
UI were a function rather than a range specifier.

So far as I can see, there is no conflict with any
existing use of square brackets in gnuplot syntax.
Neither of the existing forms
plot {ranges} ...
set ?range [...]
occurs in the context of expression evaluation,
whereas the new use can *only* occur in the context of
expression parsing. But maybe I am missing something.
Can anyone see a problem?


  • Harald Harders

    Harald Harders - 2005-06-03

    Logged In: YES

    To get a similar notation as for the sprintf function, the
    proposed _additional_ function substring(stringvar,start,end)
    could be interesting. But, the operator should also exist.

    Also, foo[x] could be interesting to get only one character.

    May be, for non-C programmers, an operator foo(beg:end),
    starting with 1 instead of 0, could be good (in addition to foo

  • Petr Mikulik

    Petr Mikulik - 2005-06-08

    Logged In: YES

    My proposals for the set of string commands:

    Returns number of words in given string.
    word(string, n)
    Returns n-th word from the string.
    (Note: it is like in REXX.)

    length(string) or strlen(string)
    Returns string length.
    index(string, substring) or strstr(string, substring)
    Returns position of the substring.

    For substring:
    OK for C and Python folk
    substr(string, from [,to])
    An alternative, to be used by awk et al folk.

  • Ethan Merritt

    Ethan Merritt - 2005-06-14

    Logged In: YES

    I've added the substring operator to cvs in both forms:
    And also a function strlen(string)

    The 14jun2005 patch implements a function
    strstrt(string,substring) which is based on the standard C
    library function strstr(). It is not exactly the same as
    strstr() in that it returns an integer which is the offset
    of the requested substring within the full string, whereas
    strstr() returns a string pointer.
    haystack = "fooneedlebaz"
    where = strstrt( haystack, "needle")
    print where
    print haystack[where:*]

    I'm still thinking about the words() and word() suggestion.
    Wouldn't it need a whitespace specifier as well -- similar
    to clib's strtok() or strsep() functions?

  • Ethan Merritt

    Ethan Merritt - 2005-06-24
    • summary: Substring operator --> Additional string functions
  • Ethan Merritt

    Ethan Merritt - 2005-06-24

    Logged In: YES

    substring syntax "foo"[beg:end] and functions substr()
    strlen() strstrt() now in cvs, so the patcheset itself is
    now out of date.

    I'm moving this to the "Feature Request" category, since I
    have not yet looked into Petr's suggestion of rexx-like
    functions words("multi-word string") and word("multi-word
    string", <word_n>)

  • Ethan Merritt

    Ethan Merritt - 2005-07-04
    • status: open --> closed-accepted
  • Ethan Merritt

    Ethan Merritt - 2005-07-04

    Logged In: YES

    added functions to cvs


Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks