Ruby String#lstrip
Remove leading spaces while preserving spacing at the other edge.
A simple example
String#lstrip
- Start with " A "
- Do this Remove leading spaces
- Result A then two spaces
value = " A "
puts("[" + value.lstrip + "]")
[A ]
Syntax
"[" + value.lstrip + "]"
Run these examples in Ruby
Save one example at a time as lesson.rb. Use Ruby 2.6 or later.
ruby lesson.rbAlign a pasted seat label
A user pastes a multi-line block from a formatted document into a plain-text field. Remove the common outer margin while preserving meaningful differences between the lines inside it.
value = " A-17 "
puts("[" + value.lstrip + "]")
[A-17 ]
The leading spaces vanish and the reserved space after 17 remains.
Clean a copied delivery field
A user pastes a name or code from another application. Remove the unwanted outer padding while preserving the spaces and letters that belong to the actual value.
value = "\tNorth Road"
puts("[" + value.lstrip + "]")
[North Road]
The tab at the beginning is removed.
Clean a course import
A course name arrives padded on both sides. Remove only the leading spaces. Brackets make any spaces left at the end visible.
What to watch for
The brackets in the output make significant trailing spaces visible.