Difference between revisions of "S2 Cookbook: Strings"

From Dreamwidth Notes
Jump to: navigation, search
Line 6: Line 6:
 
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 12:
  
 
  The length of "stylish" is 7.
 
  The length of "stylish" is 7.
 +
 +
== 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>
 +
 +
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>
 
</syntaxhighlight>
  
 
== 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 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>""";
 +
</syntaxhighlight>
  
 
[[Category: S2 Cookbook]]
 
[[Category: S2 Cookbook]]

Revision as of 09:57, 19 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>";
}

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