# TITLE: # # String NChar # # SUMMARY: # # String extensions for accessing and manipulating # a front or end number of characters. # # AUTHORS: # # - Thomas Sawyer # class String # CREDIT Lucas Carlson and Blaine Cook # def starts_with?(prefix) self.index(prefix) == 0 end # def ends_with?(suffix) self.rindex(suffix) == size - suffix.size end # Retrns _n_ characters of the string. If _n_ is positive # the characters are from the beginning of the string. # If _n_ is negative from the end of the string. # # Alternatively a replacement string can be given, which will # replace the _n_ characters. # def nchar(n, replacement=nil) if replacement s = self.dup n > 0 ? (s[0...n] = replacement) : (s[n..-1] = replacement) return s else n > 0 ? self[0...n] : self[n..-1] end end # def lchomp!(match) if index(match) == 0 self[0...match.size] = '' self end end # def lchomp(match) if index(match) == 0 self[match.size..-1] else self.dup end end end # _____ _ # |_ _|__ ___| |_ # | |/ _ \/ __| __| # | | __/\__ \ |_ # |_|\___||___/\__| # =begin test require 'test/unit' class TestStringIndex < Test::Unit::TestCase def test_nchar assert_equal( "abc", "abcxyz".nchar(3) ) assert_equal( "xyz", "abcxyz".nchar(-3) ) assert_equal( "HIxyz", "abcxyz".nchar(3, 'HI') ) assert_equal( "abcHI", "abcxyz".nchar(-3, 'HI') ) end end =end