News flash: I'm going to Australia in February, so this Web site may be switched off at any time.


Selecting a substring in Icon

A single character can be extracted from a string s by using the expression ``s[i]'', where i is the position just to the left of the character (see how Icon positions are numbered). For example, if variable ``line'' contains a line of text, then ``line[2]'' is the second character of the line.

Any part of a string s can be extracted by using the form ``s[i:j]'', where i and j are the starting and ending positions. For example, the expression ``line[4:6]'' returns the fourth and fifth characters of ``line'', and

    line[-2:0]
returns the last two characters of ``line''.

You can also use the form ``s[i+:n]'' to extract the n characters of s starting at position i. For example,

    line[5+:3]
returns the three characters starting at position 5 of line.
Next: Describing sets of characters (csets) in Icon
See also: Using the Icon programming language
Previous: Finding the size of an Icon string

John W. Shipman, john@nmt.edu