Skip to main content

String Subscription

rascal-0.40.17

Synopsis

Retrieve a substring via its index.

Syntax

Exp₁ [ Exp₂ ]

Types

Exp₁Exp₂Exp₁ [ Exp₂ ]
strintstr

Description

String subscription uses the integer value of Exp₂ as index in the string value of Exp₁. The value of Exp₂ should be greater or equal 0 and less than the number of characters in the string. If this is not the case, the exception IndexOutOfBounds is thrown.

Examples

Introduce a string, assign it to S and retrieve the element with index 1:

rascal>S = "abc";
str: "abc"
---
abc
---
rascal>S[1];
str: "b"
---
b
---

Explore an error case:

rascal>S[5];
|prompt:///|(2,1,<1,2>,<1,3>): IndexOutOfBounds(5)
at $shell$(|prompt:///|(0,5,<1,0>,<1,5>))
ok