-
Notifications
You must be signed in to change notification settings - Fork 148
Language Guide: String Operations
Added by dholton dholton
See String Interpolation. Here's a quick example:
firstname = "First"
lastname = "Last"
print "Your name is $firstname $lastname."
print "Now is $(date.Now)."
When we want to provide a string literal in its verbatim form we can use backtick "`" characters to quote the string, disabling any interpolation or formatting in it.
print `this string is not $interpolated`
# output: this string is not $interpolated
Additionally backtick quoted strings can expand multiple lines while retaining the actual whitespace in the source file. This makes them very useful for DSLs that want to process the literals:
eval `
foo = 10
print foo
`
To convert a class or a type to a string, call the ToString() method. Or if you are writing your own class you can define your own ToString() method to control how your class is printed.
//string to int
val as int
val = int.Parse("1000")
print val
//string to double
pi as double
pi = double.Parse("3.14")
print pi
//int or double to string
s as string
s = val.ToString()
print s
//multiple values to one formatted string
astr as string
astr = "$val and $pi"
print astr
//date parsing
d as date
d = date.Parse("12/03/04")
See this tutorial on the Parse and Convert techniques, as well as date parsing.
//regular comparison
if "asdf" == "ASDF":
print "asdf == ASDF"
else:
print "asdf != ASDF"
//case-insensitive comparison
if string.Compare("asdf", "ASDF", true) == 0:
print "case-insensitively the same"
s = "Another String"
if s.StartsWith("Another"):
print "starts with 'Another'"
if s.EndsWith("String"):
print "ends with 'String'"
print "'String' starts at:", s.IndexOf("String")
print "The last t is at:", s.LastIndexOf("t")
//use built-in regex support to split a string
words = @/ /.Split(s) //split on whitespace (could use \s)
for word in words:
print word
See the .NET reference on Basic String Operations for more information on string comparisons.