| 1 | = Strings and characters = |
| 2 | |
| 3 | Strings are immutable sequential collections of char objects that represent a text string. |
| 4 | |
| 5 | In Cobra code these are typed as '''String''' (on .Net this equates to System.String) |
| 6 | {{{ |
| 7 | var name as String |
| 8 | def capitalize(title as String) |
| 9 | }}} |
| 10 | |
| 11 | Within cobra you can catenate Strings explicitly with '+' |
| 12 | {{{ |
| 13 | s1 = 'my' + 'string' # after which s1 == "mystring" |
| 14 | }}} |
| 15 | |
| 16 | Cobra also supports Python-style string slicing: |
| 17 | {{{ |
| 18 | s = s[1:] # chop off first char |
| 19 | s = s[:-1] # chop off last char |
| 20 | i = s.indexOf('foo') |
| 21 | if i <> -1 |
| 22 | s = s[:i] # everything before 'foo' |
| 23 | }}} |
| 24 | Otherwise string manipulation is via the platform or Cobra runtime String and Char |
| 25 | support methods. |
| 26 | |
| 27 | An index into a string gives the position of a char object which may be the start point of |
| 28 | a substring. |
| 29 | |
| 30 | Chars are a representation of a single character (display glyph) in a string |
| 31 | (for Unicode text a Unicode character) this is usually but may not necessarily be a single |
| 32 | Char for a single glyph (see Unicode and multibyte or wide chars) |
| 33 | |
| 34 | |
| 35 | == Literals == |
| 36 | |
| 37 | === Char === |
| 38 | |
| 39 | Char literals are (usually) a single quoted character prefixed with a '''c'''. |
| 40 | {{{ |
| 41 | ch = c'x' |
| 42 | }}} |
| 43 | Can be single or double quote delimited (usually single). |
| 44 | |
| 45 | Cobra allows usual escape sequences for control characters |
| 46 | \n, \r, \t, \0 |
| 47 | {{{ |
| 48 | nlch = c'\n' |
| 49 | tabch = c'\t' |
| 50 | nulCh = c'\0' |
| 51 | }}} |
| 52 | |
| 53 | === String === |
| 54 | Literal Strings are (single) single quote or double quote delimited |
| 55 | ('string' or "another string") |
| 56 | |
| 57 | All strings are single line (currently). |
| 58 | |
| 59 | A [] delimited expression in a string indicates interpolation or substitution of enclosed |
| 60 | expression at that point. |
| 61 | {{{ |
| 62 | x='hops' |
| 63 | print 'hi [x]' # outputs: hi hops |
| 64 | print "hi [x]" # also outputs: hi hops |
| 65 | |
| 66 | print 'Hi [customer.firstName] [customer.lastName]' |
| 67 | }}} |
| 68 | leading [ can be escaped '\[' to escape this substitution. |
| 69 | |
| 70 | |
| 71 | Literal strings support the usual escape characters. |
| 72 | \n - newline. |
| 73 | \r - carriage return. |
| 74 | \t - tab. |
| 75 | \0 - nul char. |
| 76 | |
| 77 | (arbitrary embedded hex or unicode ??) |
| 78 | |
| 79 | ==== Raw Strings ==== |
| 80 | {{{r'string'}}} - unexpanded ('raw') string |
| 81 | |
| 82 | Does not interpret escape chars or substitute expressions in [] |
| 83 | |
| 84 | ==== non Substituted strings ==== |
| 85 | {{{ns'string[x]'}}} or {{{ns"string[x]"}}} |
| 86 | |
| 87 | Does not do expression substitution - items in [] are treated as text |
| 88 | |
| 89 | Interprets escape chars but suppresses expression interpolation/substitution |
| 90 | (handling of []) |
| 91 | |
| 92 | == !DocStrings == |
| 93 | Single or multiline """ delimited text intended to document the item. |
| 94 | |
| 95 | Terminating trigraph must be on new line (after initiating line) or (single line docstring ) at end on same as start line. |
| 96 | |
| 97 | Allowed on File, (or after) class, struct, interface, var, def, sig, event, methods. |
| 98 | Usual form |
| 99 | {{{ |
| 100 | """ |
| 101 | Heres the doc string |
| 102 | """ |
| 103 | }}} |
| 104 | e.g. |
| 105 | {{{ |
| 106 | class BigOne |
| 107 | """ |
| 108 | Implementation of a BigOne thingy... |
| 109 | yadda yadda yadda |
| 110 | """ |
| 111 | |
| 112 | var _initVal |
| 113 | "The initial BigOne value" |
| 114 | }}} |
| 115 | |
| 116 | |
| 117 | |
| 118 | == Platform == |
| 119 | |
| 120 | There is another string form to allow inline c# src code to be inserted at |
| 121 | the occurring position in generated source. |
| 122 | |
| 123 | {{{sharp"c# src code"}}} or {{{sharp'c# src code'}}} |
| 124 | |
| 125 | e.g. |
| 126 | {{{ |
| 127 | t = sharp'System.Type.GetType(qualifiedName)' |
| 128 | }}} |
| 129 | == Examples == |
| 130 | {{{ |
| 131 | s = 'A simple String' |
| 132 | |
| 133 | i=10 |
| 134 | ssub = 'result is:[i]' # ssub gets 'result is:10' |
| 135 | |
| 136 | ns0 = ns'result is:[i]' # ns gets 'result is:[i]' |
| 137 | ns1 = 'result is:\[i]' # ns1 gets 'result is:[i]' |
| 138 | |
| 139 | rs = r'here [is]\nsome\r' # rs gets string 'here [is]\nsome\r' |
| 140 | }}} |
| 141 | == Notes == |
| 142 | |
| 143 | Idiomatically most textstrings in cobra use single-quote ' as delimiters unless need to |
| 144 | enclose a '. |
| 145 | |
| 146 | Exact String and Char representation is platform dependent as are support and utility [[BR]] |
| 147 | methods pending a complete portable Runtime library.[[BR]] |
| 148 | In the meantime we are using platform (.Net and Mono) capabilities and semantics. |
| 149 | |
| 150 | Eventually there will be support for multiline text strings (embedded newlines from source representation). |
| 151 | |