Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Groovy

Idiom #46 Extract beginning of string (prefix)

Create the string t consisting of the 5 first characters of the string s.
Make sure that multibyte characters are properly handled.

def t = s.take(5)

Returns the entire string if shorter than five characters.
def t = s[0..<5]

Uses a half inclusive range. Will throw an exception if s is shorter than 5 characters.
T : String := S (1 .. 5);

New implementation...