Wiki
Version 4 (modified by hopscc, 4 years ago)

--

Strings and characters

Strings are immutable sequential collections of char objects that represent a text string.

In Cobra code these are typed as String (on .Net this equates to System.String)

var name as String
def capitalize(title as String)

Within cobra you can catenate Strings explicitly with '+'

s1 = 'my' + 'string'  # after which s1 == "mystring"

Cobra also supports Python-style string slicing:

s = s[1:]  # chop off first char
s = s[:-1] # chop off last char
i = s.indexOf('foo')
if i <> -1
    s = s[:i]  # everything before 'foo'

Otherwise string manipulation is via the platform or Cobra runtime String and Char support methods.

An index into a string gives the position of a char object which may be the start point of a substring.

Chars are a representation of a single character (display glyph) in a string (for Unicode text a Unicode character) this is usually but may not necessarily be a single Char for a single glyph (see Unicode and multibyte or wide chars)

Literals

Char

Char literals are (usually) a single quoted character prefixed with a c.

ch = c'x'

Can be single or double quote delimited (usually single).

Cobra allows usual escape sequences for control characters

  • \a - alarm (bell/beep)
  • \b - backspace
  • \f - formfeed
  • \n - newline.
  • \r - carriage return.
  • \t - tab.
  • \v - vertical tab
  • \\ - single \
  • \' - single quote '
  • \" - double quote "
  • \0 - nul char.
    nlch    = c'\n'  
    tabch   = c'\t'
    nulCh   = c'\0' 
    

String

Literal Strings are (single) single quote or double quote delimited ('string' or "another string")

All strings are single line (currently).

A [] delimited expression in a string indicates interpolation or substitution of enclosed expression at that point.

x='hops'
print 'hi [x]'  # outputs: hi hops
print "hi [x]"  # also outputs: hi hops

print 'Hi [customer.firstName] [customer.lastName]'

leading [ can be escaped '\[' to escape this substitution (individually).

The expression inside the [] may have an optional format descriptor following the expression and separated from it by a ':'
Any of the (.Net) single, standard or Custom format descriptors can be given.
(see  IFormattable interface)

# std 
x = 123.456
print 'val is [x] or [x:F4]' # val is 123.456 or 123.4560

x1=123
print 'val [x1] is [x1:D4]'   # val [123] is 0123
print 'hex is 0x[x1:x4]' # hex is 0x007b

# custom
n=67263807
print 'PhoneNum is [n:00-0000-000]' # PhoneNum is 06-7263-807
#std datetime
d = DateTime()
print 'dflt is [d:F]' # dflt is  Monday, 01 January 0001 00:00:00

Literal strings support the usual escape characters.

  • \a - alarm (bell/beep)
  • \b - backspace
  • \f - formfeed
  • \n - newline.
  • \r - carriage return.
  • \t - tab.
  • \v - vertical tab
  • \\ - single \
  • \' - single quote '
  • \" - double quote "
  • \0 - nul char.

(arbitrary embedded hex or unicode ??)

Raw Strings

r'string' - unexpanded ('raw') string

Does not interpret escape chars or substitute expressions in []

non Substituted strings

ns'string[x]' or ns"string[x]"

Does not do expression substitution - items in [] are treated as text

Interprets escape chars but suppresses expression interpolation/substitution (expansion of expressions in [])

DocStrings

Single or multiline """ delimited text intended to document the item.

Terminating trigraph must be on new line (after initiating line) or (single line docstring ) at end on same as start line.

Allowed on File, (or after) class, struct, interface, var, def, sig, event, methods.

Usual form

""" Heres a single line docString."""
...
"""
Heres a multiline doc string.
Second and subsequent lines follow
...
"""

"""
Heres another single line docString.
"""

e.g.

class BigOne
    """
    Implementation of a BigOne thingy...
    yadda yadda yadda
    """

    var _initVal
        """The initial BigOne value"""

Platform

There is another string form to allow inline c# src code to be inserted at the occurring position in generated source.

sharp"c# src code" or sharp'c# src code'

e.g.

t = sharp'System.Type.GetType(qualifiedName)'

Examples

s = 'A simple String'

i=10
ssub  = 'result is:[i]'    # ssub gets 'result is:10'

ns0 = ns'result is:[i]'    # ns   gets 'result is:[i]' 
ns1 =   'result is:\[i]'   # ns1  gets 'result is:[i]' 

rs = r'here [is]\nsome\r'  # rs gets string 'here [is]\nsome\r'

Notes

Idiomatically most textstrings in cobra use single-quote ' as delimiters unless need to enclose a '.

Exact String and Char representation is platform dependent as are support and utility
methods pending a complete portable Runtime library.
In the meantime we are using platform (.Net and Mono) capabilities and semantics.

Eventually there will be support for multiline text strings (embedded newlines from source representation).