-
Notifications
You must be signed in to change notification settings - Fork 148
Language Guide: String Operations
scottstephens edited this page Jul 24, 2012
·
5 revisions
See [Language-Guide:-String-Interpolation). Here's a quick example:
firstname = "First"
lastname = "Last"
print "Your name is $firstname $lastname."
print "Now is $(date.Now)."
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.