General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".


Postby Charles » Wed Mar 05, 2008 12:47 pm

I received an email asking about strings. I'm posting the message and reply here:

> I’ve read almost everything available on Cobra and I’ve begun executing the
> HowTo and Sample programs.
> In D the addition of a Concatenation operator greatly facilitates the
> handling of strings. Will Cobra handle strings on a par with D? I hope so.

I believe so, but my knowledge of D is getting rusty as I investigated it over 2 years ago.

In Cobra, you can concatenate two strings with "+" as in "a + b". You can also substitute expressions into strings by surrounding them with square brackets. And you can add .NET formatting codes using a colon as seen below:
print 'Hi [customer.firstName] [customer.lastName]'

print 'The rate is [amount/capacity*100:N2]%.' # example: The rate is 85.47%.

s = 'Point([.x:N2], [.y:N2])'

print r'Raw strings are prefixed by "r" and do not interpret brackets [] or escaped chars like \n'

print ns'No-substitution strings are prefix by "ns" and do not interpret brackets [].\n\n\n'

print "Strings can be double quoted. There is no difference in treatment."

# chars are prefixed with a c
ch = c'x'

The String class is pretty mature as it comes straight out of the .NET/Mono library.

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]

For more information on formatting codes, you can search the web for things like "C# numeric formatting" or "Visual Basic date formatting".

For the String docs, refer to the MSDN String Class or more specifically the MSDN String Members.

You can also use .NET/Mono regular expression classes.

If I missed something or you have a further questions, let me know.
Posts: 2515
Location: Los Angeles, CA

Return to Discussion

Who is online

Users browsing this forum: No registered users and 9 guests