if a string begins with a bullet or dash, remove it. in ruby

You can use regular expressions to remove any bullet or dash at the beginning of a string. Here's an example code snippet:

main.rb
str = "- This is a string with a bullet at the beginning"
str = str.sub(/^[*-]\s+/, '')
puts str #=> "This is a string with a bullet at the beginning"
151 chars
4 lines

In this example, sub is used to substitute the matched pattern in the string with an empty string, effectively removing it. The regular expression (/^[*-]\s+/) matches any string that starts with an asterisk or dash, followed by one or more whitespace characters. The ^ metacharacter denotes the start of the string.

related categories

gistlibby LogSnag