String constants, string variables, and string functions

In addition to string constants, most gnuplot commands also accept a string variable, a string expression, or a function that returns a string. For example, the following four methods of creating a plot all result in the same plot title:
four = "4"
graph4 = "Title for plot #4"
graph(n) = sprintf("Title for plot #%d",n)
plot 'data.4' title "Title for plot #4"
plot 'data.4' title graph4
plot 'data.4' title "Title for plot #".four
plot 'data.4' title graph(4)

Since integers are promoted to strings when operated on by the string concatenation operator ('.' character), the following method also works:

N = 4
plot 'data.'.N title "Title for plot #".N

In general, elements on the command line will only be evaluated as possible string variables if they are not otherwise recognizable as part of the normal gnuplot syntax. So the following sequence of commands is legal, although probably should be avoided so as not to cause confusion:

plot = "my_datafile.dat"
title = "My Title"
plot plot title title

Substrings

Substrings can be specified by appending a range specifier to any string, string variable, or string-valued function. The range specifier has the form [begin:end], where begin is the index of the first character of the substring and end is the index of the last character of the substring. The first character has index 1. The begin or end fields may be empty, or contain '*', to indicate the true start or end of the original string. E.g. str[:] and str[*:*] both describe the full string str.

String operators

Three binary operators require string operands: the string concatenation operator ".", the string equality operator "eq" and the string inequality operator "ne". The following example will print TRUE.
if ("A"."B" eq "AB") print "TRUE"

String functions

Gnuplot provides several built-in functions that operate on strings. General formatting functions: see gprintf sprintf. Time formatting functions: see strftime strptime. String manipulation: see substr strstrt trim word words.

String encoding

Gnuplot's built-in string manipulation functions are sensitive to utf-8 encoding (see set encoding). For example
utf8string = "αβγ"
strlen(utf8string) returns 3 (number of characters, not number of bytes)
utf8string[2:2] evaluates to "β"
strstrt(utf8string,"β") evaluates to 2

Copyright 1986 - 1993, 1998, 2004 Thomas Williams, Colin Kelley
Distributed under the gnuplot license (rights to distribute modified versions are withheld).