Difference between revisions of "S2 Cookbook: Strings"

From Dreamwidth Notes
Jump to: navigation, search
 
(10 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== Get the length of a string ==
+
== Declaring a string variable ==
 +
 
 +
<syntaxhighlight lang="s2">
 +
var string insert = "moo";
 +
</syntaxhighlight>
 +
 
 +
== Printing out a string ==
 +
 
 +
You can use the <code>print</code> keyword to print out a string:
 +
 
 +
<syntaxhighlight lang="s2">
 +
print """Once upon a time...""";
 +
</syntaxhighlight>
 +
 
 +
But, just putting a string as a statement by itself will also print it out:
 +
 
 +
<syntaxhighlight lang="s2">
 +
"""Once upon a time...""";
 +
</syntaxhighlight>
 +
 
 +
If you are working on a system style, you can use the <code>print safe</code> command to print out trusted strings without filtering (increasing inefficiency):
 +
 
 +
<syntaxhighlight lang="s2">
 +
print safe """Once upon a time...""";
 +
</syntaxhighlight>
 +
 
 +
== Using a variable inside of a string ==
 +
 
 +
If you put a variable into a string, it'll be replaced with the contents of the variable:
 +
 
 +
<syntaxhighlight lang="s2">
 +
var string insert = "moo";
 +
 
 +
# "moo cow"
 +
"""$insert cow\n""";
 +
</syntaxhighlight>
 +
 
 +
Sometimes, though, you need to use a variable next to valid variable name characters.  In that case, you can enclose the variable name in ${} and have it still work:
 +
 
 +
<syntaxhighlight lang="s2">
 +
var string insert = "moo";
 +
 
 +
# "moocow"
 +
"""${insert}cow\n""";
 +
</syntaxhighlight>
 +
 
 +
== Using a $ in a string ==
 +
 
 +
But what if you want to use a $ in a string and not a variable?  Make sure to escape it: <code>\$</code>
 +
 
 +
<syntaxhighlight lang="s2">
 +
"""\$tester\n""";
 +
</syntaxhighlight>
 +
 
 +
 
 +
== Getting the length of a string ==
  
 
<syntaxhighlight lang="s2">
 
<syntaxhighlight lang="s2">
Line 6: Line 61:
 
var int length = $x->length();
 
var int length = $x->length();
 
# Print out what the length is
 
# Print out what the length is
print """The length of "$x" is $length."""
+
"""The length of "$x" is ${length}.""";
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 12: Line 67:
  
 
  The length of "stylish" is 7.
 
  The length of "stylish" is 7.
 +
 +
== Concatenating strings ==
 +
 +
<syntaxhighlight lang="s2">
 +
var string fragment = "I am a ";
 +
var string foxy = "quick brown fox";
 +
var string sentence = $fragment + $foxy + ".";
 +
 +
# I am a quick brown fox.
 +
print "<p>$sentence</p>";
 +
</syntaxhighlight>
 +
 +
== Finding if a string starts or ends with another string ==
 +
 +
<syntaxhighlight lang="s2">
 +
var string foxy = "fox.png";
 +
 +
if( $foxy->ends_with( ".png" ) ) {
 +
    print "<p>This is a PNG file.</p>";
 +
}
 +
 +
if( $foxy->starts_with( "fox" ) ) {
 +
    print "<p>This is a picture of a fox.</p>";
 +
}</syntaxhighlight>
  
 
== Finding a string in another string ==
 
== Finding a string in another string ==
 +
 +
If you're just wanting to know if a string is in another string, use the <tt>contains</tt> string function:
  
 
<syntaxhighlight lang="s2">
 
<syntaxhighlight lang="s2">
 
var string foxy = "I am a quick brown fox.";
 
var string foxy = "I am a quick brown fox.";
  
 +
# The string contains "fox", so the statement prints
 +
if( $foxy->contains("fox") ) {
 +
    print "<p>I am a fox.</p>";
 +
}
 +
 +
# This string doesn't contain "wolf", so the statement doesn't print
 +
if( $foxy->contains("wolf") ) {
 +
    print "<p>I am a wolf.</p>";
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
If you need to find the actual location of your substring in a string, you'll want to use the <tt>index</tt> string function.
 +
 +
<syntaxhighlight lang="s2">
 +
 +
var string foxy = "I am a quick brown fox.";
 +
 +
# This will print out "Location of fox: 19."
 +
var int fox_location = $foxy->index("fox");
 +
if( $fox_location >= 0 ) {
 +
    print "<p>Location of fox: ${fox_location}.</p>";   
 +
} else {
 +
    print "<p>No fox to be found.</p>";
 +
}
 +
 +
# This will print out "No wolf to be found."
 +
var int wolf_location = $foxy->index("wolf");
 +
if( $wolf_location >= 0 ) {
 +
    print "<p>Location of wolf: ${wolf_location}.</p>";
 +
} else {
 +
    print "<p>No wolf to be found.</p>";
 +
}
 +
</syntaxhighlight>
 +
 +
When you test to see if the substring was found in the string, remember that the <tt>index</tt> function starts counting from zero, not one, and returns -1 if it can't find a match.
  
 
== Changing the case of a string ==
 
== Changing the case of a string ==
 +
 +
There are three functions for this, <tt>lower</tt>, <tt>upper</tt>, and <tt>upperfirst</tt>.
 +
 +
<syntaxhighlight lang="s2">
 +
var string lowercase = "fox";
 +
var string proper = "Polite";
 +
 +
print "<p>Original lowercase: $lowercase</p>";
 +
print "<p>Uppercase: " + $lowercase->upper() + "</p>";
 +
print "<p>Upper first: " + $lowercase->upperfirst() + "</p>";
 +
print "<p>Original uppercased: $proper</p>";
 +
print "<p>Lowercase: " + $proper->lower() + "</p>";
 +
</syntaxhighlight>
  
 
== Comparing two strings to see if they are equal or not equal ==
 
== Comparing two strings to see if they are equal or not equal ==
  
== Comparing two strings lexographically ==
+
<syntaxhighlight lang="s2">
 +
var string foxy = "fox";
 +
 
 +
if( $foxy == "fox" ) {
 +
    print "<p>This is a fox.</p>";
 +
} else {
 +
    print "<p>This is not a fox.</p>";
 +
}
 +
</syntaxhighlight>
 +
 
 +
== Comparing two strings lexicographically (aka alphabetical order) ==
 +
 
 +
<syntaxhighlight lang="s2">
 +
var string foxy = "fox";
 +
var string wolfish = "wolf";
 +
var int compare = $foxy->compare($wolfish);
 +
 
 +
# Compare: 1
 +
print "<p>Compare: $compare</p>";
 +
 
 +
# fox comes before wolf
 +
if( $compare == 0 ) {
 +
    print "<p>$foxy and $wolfish are the same</p>";
 +
} elseif ( $compare > 0 ) {
 +
    print "<p>$foxy comes before $wolfish</p>";
 +
} elseif ( $compare < 0 ) {
 +
    print "<p>$foxy comes after $wolfish</p>";
 +
}
 +
</syntaxhighlight>
  
 
== Splitting a string into pieces ==
 
== Splitting a string into pieces ==
 +
 +
<syntaxhighlight lang="s2">
 +
var string sentence = "one fish two fish  red cow";
 +
var string[] list = $sentence->split(" ");
 +
 +
print "<p>";
 +
# "one", "fish", "two", "fish", "", "red", "cow",
 +
foreach var string item ($list) {
 +
    print """ "$item", """;
 +
}
 +
print "</p>";
 +
</syntaxhighlight>
  
 
== Repeating a string ==
 
== Repeating a string ==
  
== Alternate between two strings ==
+
<syntaxhighlight lang="s2">
 +
var string sentence = "I am a quick brown fox.";
 +
var int length = $sentence->length();
 +
var string underline = "-";
 +
 +
# Print out the sentence.
 +
"""<p style="font-family: courier;">$sentence""";
 +
 
 +
# Underline the sentence using the $underline string.
 +
"<br />" + $underline->repeat( $length ) + "</p>";
 +
</syntaxhighlight>
 +
 
 +
Will print out:
 +
 
 +
I am a quick brown fox.
 +
-----------------------
 +
 
 +
== Replacing a string inside of another string ==
 +
 
 +
<syntaxhighlight lang="s2">
 +
var string foxy = "fox";
 +
var string wolfish = "wolf";
 +
var string sentence = "I am a wolf, hunted by wolfhounds.";
 +
 
 +
# I am a wolf, hunted by wolfhounds.
 +
print "<p>$sentence</p>";
 +
 
 +
# I am a fox, hunted by foxhounds.
 +
print "<p>" + $sentence->replace($wolfish, $foxy) + "</p>";
 +
</syntaxhighlight>
 +
 
 +
== Reversing a string ==
 +
 
 +
<syntaxhighlight lang="s2">
 +
var string foxy = "I am a quick brown fox.";
 +
var string backwards_foxy = reverse $foxy;
 +
 
 +
# I am a quick brown fox.
 +
"""<p>$foxy</p>""";
 +
 
 +
# .xof nworb kciuq a ma I
 +
"""<p>$backwards_foxy</p>""";
 +
</syntaxhighlight>
  
 
[[Category: S2 Cookbook]]
 
[[Category: S2 Cookbook]]

Latest revision as of 05:48, 12 June 2011

Declaring a string variable

var string insert = "moo";

Printing out a string

You can use the print keyword to print out a string:

print """Once upon a time...""";

But, just putting a string as a statement by itself will also print it out:

"""Once upon a time...""";

If you are working on a system style, you can use the print safe command to print out trusted strings without filtering (increasing inefficiency):

print safe """Once upon a time...""";

Using a variable inside of a string

If you put a variable into a string, it'll be replaced with the contents of the variable:

var string insert = "moo";
 
# "moo cow"
"""$insert cow\n""";

Sometimes, though, you need to use a variable next to valid variable name characters. In that case, you can enclose the variable name in ${} and have it still work:

var string insert = "moo";
 
# "moocow"
"""${insert}cow\n""";

Using a $ in a string

But what if you want to use a $ in a string and not a variable? Make sure to escape it: \$

"""\$tester\n""";


Getting the length of a string

var string x = "stylish";
# Get the length of our example string
var int length = $x->length();
# Print out what the length is
"""The length of "$x" is ${length}.""";

Will print out:

The length of "stylish" is 7.

Concatenating strings

var string fragment = "I am a ";
var string foxy = "quick brown fox";
var string sentence = $fragment + $foxy + ".";
 
# I am a quick brown fox.
print "<p>$sentence</p>";

Finding if a string starts or ends with another string

var string foxy = "fox.png";
 
if( $foxy->ends_with( ".png" ) ) {
    print "<p>This is a PNG file.</p>";
}
 
if( $foxy->starts_with( "fox" ) ) {
    print "<p>This is a picture of a fox.</p>";
}

Finding a string in another string

If you're just wanting to know if a string is in another string, use the contains string function:

var string foxy = "I am a quick brown fox.";
 
# The string contains "fox", so the statement prints
if( $foxy->contains("fox") ) {
    print "<p>I am a fox.</p>";
}
 
# This string doesn't contain "wolf", so the statement doesn't print
if( $foxy->contains("wolf") ) {
    print "<p>I am a wolf.</p>";
}

If you need to find the actual location of your substring in a string, you'll want to use the index string function.

var string foxy = "I am a quick brown fox.";
 
# This will print out "Location of fox: 19."
var int fox_location = $foxy->index("fox");
if( $fox_location >= 0 ) {
    print "<p>Location of fox: ${fox_location}.</p>";    
} else {
    print "<p>No fox to be found.</p>";
}
 
# This will print out "No wolf to be found."
var int wolf_location = $foxy->index("wolf");
if( $wolf_location >= 0 ) {
    print "<p>Location of wolf: ${wolf_location}.</p>";
} else {
    print "<p>No wolf to be found.</p>";
}

When you test to see if the substring was found in the string, remember that the index function starts counting from zero, not one, and returns -1 if it can't find a match.

Changing the case of a string

There are three functions for this, lower, upper, and upperfirst.

var string lowercase = "fox";
var string proper = "Polite";
 
print "<p>Original lowercase: $lowercase</p>";
print "<p>Uppercase: " + $lowercase->upper() + "</p>";
print "<p>Upper first: " + $lowercase->upperfirst() + "</p>";
print "<p>Original uppercased: $proper</p>";
print "<p>Lowercase: " + $proper->lower() + "</p>";

Comparing two strings to see if they are equal or not equal

var string foxy = "fox";
 
if( $foxy == "fox" ) {
    print "<p>This is a fox.</p>";
} else {
    print "<p>This is not a fox.</p>";
}

Comparing two strings lexicographically (aka alphabetical order)

var string foxy = "fox";
var string wolfish = "wolf";
var int compare = $foxy->compare($wolfish);
 
# Compare: 1
print "<p>Compare: $compare</p>";
 
# fox comes before wolf
if( $compare == 0 ) {
    print "<p>$foxy and $wolfish are the same</p>";
} elseif ( $compare > 0 ) {
    print "<p>$foxy comes before $wolfish</p>";
} elseif ( $compare < 0 ) {
    print "<p>$foxy comes after $wolfish</p>";
}

Splitting a string into pieces

var string sentence = "one fish two fish  red cow";
var string[] list = $sentence->split(" ");
 
print "<p>";
# "one", "fish", "two", "fish", "", "red", "cow", 
foreach var string item ($list) {
    print """ "$item", """;
}
print "</p>";

Repeating a string

var string sentence = "I am a quick brown fox.";
var int length = $sentence->length();
var string underline = "-";
 
# Print out the sentence.
"""<p style="font-family: courier;">$sentence""";
 
# Underline the sentence using the $underline string.
"<br />" + $underline->repeat( $length ) + "</p>";

Will print out:

I am a quick brown fox.
-----------------------

Replacing a string inside of another string

var string foxy = "fox";
var string wolfish = "wolf";
var string sentence = "I am a wolf, hunted by wolfhounds.";
 
# I am a wolf, hunted by wolfhounds.
print "<p>$sentence</p>";
 
# I am a fox, hunted by foxhounds.
print "<p>" + $sentence->replace($wolfish, $foxy) + "</p>";

Reversing a string

var string foxy = "I am a quick brown fox.";
var string backwards_foxy = reverse $foxy;
 
# I am a quick brown fox.
"""<p>$foxy</p>""";
 
# .xof nworb kciuq a ma I
"""<p>$backwards_foxy</p>""";