Library contents

Ruby String#lstrip

Remove leading spaces while preserving spacing at the other edge.

A simple example

String#lstrip

  1. Start with " A "
  2. Do this Remove leading spaces
  3. Result A then two spaces
Only the spaces before A disappear.
lesson.rb
value = "  A  "
puts("[" + value.lstrip + "]")
Output
[A  ]

Syntax

Ruby 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.rb

Align 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.

From the programvalue = " A-17 "
Result[A-17 ]
lesson.rb
value = "  A-17 "
puts("[" + value.lstrip + "]")
Output
[A-17 ]

The leading spaces vanish and the reserved space after 17 remains.

Advertisements

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.

From the programvalue = "\tNorth Road"
Result[North Road]
lesson.rb
value = "\tNorth Road"
puts("[" + value.lstrip + "]")
Output
[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.

Given' Art '
Worked result[Art ]

What to watch for

The brackets in the output make significant trailing spaces visible.

Documentation for String#lstrip