Difference between revisions of "S2 Cookbook: Strings"

From Dreamwidth Notes
Jump to: navigation, search
(Fixed code sample bug: index function counts from zero, so test with >= 0)
Line 52: Line 52:
 
# This will print out "Location of fox: 19."
 
# This will print out "Location of fox: 19."
 
var int fox_location = $foxy->index("fox");
 
var int fox_location = $foxy->index("fox");
if( $fox_location > 0 ) {
+
if( $fox_location >= 0 ) {
 
     print "<p>Location of fox: ${fox_location}.</p>";     
 
     print "<p>Location of fox: ${fox_location}.</p>";     
 
} else {
 
} else {
Line 60: Line 60:
 
# This will print out "No wolf to be found."
 
# This will print out "No wolf to be found."
 
var int wolf_location = $foxy->index("wolf");
 
var int wolf_location = $foxy->index("wolf");
if( $wolf_location > 0 ) {
+
if( $wolf_location >= 0 ) {
 
     print "<p>Location of wolf: ${wolf_location}.</p>";
 
     print "<p>Location of wolf: ${wolf_location}.</p>";
 
} else {
 
} else {
Line 66: Line 66:
 
}
 
}
 
</syntaxhighlight>
 
</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 ==

Revision as of 07:20, 23 June 2010

Get 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.

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 word = "replace me";
var int length = $word->length();
var string flourish = "*";
 
# I am replacing "replace me" with "**********".
"""<p>I am replacing "$word" with \"""" + $flourish->repeat( $length ) + """\".</p>""";

Replacing a string inside of another string

Reversing a string