What is lstrip in Ruby?
Overview
lstrip in Ruby is used to remove the leading whitespace characters in a string.
A whitespace character could be a space, null, horizontal tab, line feed, vertical tab, form feed, or carriage return.
Syntax
str.lstrip
where str is the string whose leading whitespace characters we want to remove.
Parameters
This function doesn’t take any parameters.
Return value
It returns the string str with any leading whitespace characters removed.
Code example
# create stringsstr1 = " Edpresso"str2 = "is"str3 = "\n\t\rThe"str4 = "\Best !"# remove leading# whitespace charactersa = str1.lstripb = str2.lstripc = str3.lstripd = str4.lstrip# print resultsputs aputs bputs cputs d
Explanation
-
Lines 2-5: We create some strings from which we want to remove the leading whitespace characters.
-
Lines 9-12: We use the
lstripmethod to remove the leading whitespace characters from the strings and store the returned values in variablesa,b,c, andd. -
Lines 15-18: We print the results to the console.