S2 Cookbook: Strings

From Dreamwidth Notes
Jump to: navigation, search

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>""";