# -*- coffee -*- # @@PLEAC@@_NAME # @@SKIP@@ CoffeeScript # @@PLEAC@@_WEB # @@SKIP@@ http://jashkenas.github.com/coffee-script # @@PLEAC@@_INTRO # @@SKIP@@ I use CoffeeScript version 1.1.0 to interactively debug the code. # @@PLEAC@@_1.0 #----------------------------- string = '\\n' # two characters, \ and an n string = "\\n" # two characters, \ and an n string = 'Jon \'Maddog\' Orwant' # literal single quotes string = "Jon 'Maddog' Orwant" # literal single quotes #----------------------------- string = "\n" # a "newline" character string = '\n' # a "newline" character string = "Jon \"Maddog\" Orwant" # literal double quotes string = 'Jon "Maddog" Orwant' # literal double quotes #----------------------------- # @@PLEAC@@_1.1 value = string.substring offset, count value = string.substring offset #----------------------------- string = "This is what you have" first = string.substring 0, 1 # "T" start = string.substring 5, 2 # "is" rest = string.substring 13 # "you have" last = string.substring string.length - 1 # "e" end = string.substring string.length - 4 # "have" piece = string.substring string.length - 8, 16 # "you" #----------------------------- # you can test substrings with match() if string.substring(10).match /pattern/ console.log "Pattern matches in last 10 characters\n" #----------------------------- cut2fmt = (positions) -> template = '' lastpos = 1 for place in positions template += "A" + (place - lastpos) + " " lastpos = place template += "A*" fmt = cut2fmt([8, 14, 20, 26, 30]) console.log fmt # A7 A6 A6 A6 A4 A* #-----------------------------