Before (String function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Template:String:Before subtitle}}
{{Template:String:Before subtitle}}
<var>Before</var> operates on a string and returns the portion of that string prior to the user-specified delimiter.  To get the portion of the string after a delimiter, use the <var>[[After (String function)|After]]</var> string method.


<var>Before</var> operates on a string and returns the portion of that string prior to the user-specified delimiter.  To get the portion of the string after a delimiter, use the <var>[[After (String function)|After]]</var> string method.
==Syntax==
==Syntax==
{{Template:String:Before syntax}}
{{Template:String:Before syntax}}
===Syntax terms===
===Syntax terms===
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th>%outString</th><td>string</td></tr>
<tr><th nowrap>%outString</th>
<td>A <var>String</var> or <var>Longstring</var> variable that must be long enough to hold the resulting parsed string. If a defined string length is shorter than needed to hold the result, a request canceling error occurs.</td></tr>
 
<tr><th>string</th>
<tr><th>string</th>
<td>string</td></tr>
<td>A <var>String</var> or <var>Longstring</var> variable that holds the string to be parsed.</td></tr>
 
<tr><th>substring</th>
<tr><th>substring</th>
<td>string</td></tr>
<td>A <var>String</var> or <var>Longstring</var> variable that holds the separator character or characters on which parsing occurs.</td></tr>
 
<tr><th><var>Start</var></th>
<tr><th><var>Start</var></th>
<td>number<br/>This default value of this argument is [[??]].</td></tr>
<td>A number that is the starting point for parsing. The default value is 1, indicating the beginning of the string.  This is a [[Notation_conventions_for_methods#Named_arguments|name allowed]] argument, and it cannot be 0.  If a value larger than the string is specified, the method returns a null string.</td></tr>
</table>
</table>
==Usage notes==
==Usage notes==
<ul>
<li>As the examples below show, if a starting value is entered, the <var class="term">%outString</var> value is the content of the string beginning at the specified start point and ending at either of these:
<ul>
<li>The character before the first delimiter encountered after the start point.
<li>The end of the string. 
</ul> </li>
<li>The delimiter string is <i>not</i> a list of individual delimiters, but a single delimiter which may be multiple characters. In other words, if your delimiter is <code>ok</code>, the method looks for occurrences of those two lowercase letters and returns a value from the string that precedes the occurrence of those letters. </li>
<li><var>Before</var> is always case-sensitive. </li>
</ul>


==Examples==
==Examples==
The following request prints  
The following request prints  
<p><code>I am the Eggman</code></p>
<code>I am the Eggman</code>:
<p class="code">begin
<p class="code">begin
   %x is string len 64 initial('I am the Eggman!  I am the Walrus!  Koo Koo Kachoo!')
   %x is string len 64 initial('I am the Eggman!  I am the Walrus!  Koo Koo Kachoo!')
Line 26: Line 42:
</p>
</p>


The following request prints  
The following request prints <code>Eggman</code>:
<p><code>Eggman!</code></p>
<p class="code">begin
<p class="code">begin
   %x is string len 64 initial('I am the Eggman!  I am the Walrus!  Koo Koo Kachoo!')
   %x is string len 64 initial('I am the Eggman!  I am the Walrus!  Koo Koo Kachoo!')
   printText {%x:before('!',start=10)}
   printText {%x:before('!',start=10)}
end
</p>
The following prints each blank-delimited word, one at a time:
<p class="code">begin
  %x is string len 64 initial('I am the Eggman!  I am the Walrus!  Koo Koo Kachoo!')
  repeat while %x:length
    print %x:before(' ')
    %x = %x:after(' ')
  end repeat
end
end
</p>
</p>


==See also==
==See also==
<ul>
<li><var>[[After (String function)|After]]</var></li>
</ul>
{{Template:String:Before footer}}
{{Template:String:Before footer}}
The <var>[[After (String function)|Before]]</var> string method.

Latest revision as of 20:04, 3 September 2015

Part of string before a substring (String class)

[Introduced in Model 204 7.5]

Before operates on a string and returns the portion of that string prior to the user-specified delimiter. To get the portion of the string after a delimiter, use the After string method.

Syntax

%outString = string:Before( substring, [Start= number])

Syntax terms

%outString A String or Longstring variable that must be long enough to hold the resulting parsed string. If a defined string length is shorter than needed to hold the result, a request canceling error occurs.
string A String or Longstring variable that holds the string to be parsed.
substring A String or Longstring variable that holds the separator character or characters on which parsing occurs.
Start A number that is the starting point for parsing. The default value is 1, indicating the beginning of the string. This is a name allowed argument, and it cannot be 0. If a value larger than the string is specified, the method returns a null string.

Usage notes

  • As the examples below show, if a starting value is entered, the %outString value is the content of the string beginning at the specified start point and ending at either of these:
    • The character before the first delimiter encountered after the start point.
    • The end of the string.
  • The delimiter string is not a list of individual delimiters, but a single delimiter which may be multiple characters. In other words, if your delimiter is ok, the method looks for occurrences of those two lowercase letters and returns a value from the string that precedes the occurrence of those letters.
  • Before is always case-sensitive.

Examples

The following request prints I am the Eggman:

begin %x is string len 64 initial('I am the Eggman! I am the Walrus! Koo Koo Kachoo!') printText {%x:before('!')} end

The following request prints Eggman:

begin %x is string len 64 initial('I am the Eggman! I am the Walrus! Koo Koo Kachoo!') printText {%x:before('!',start=10)} end

The following prints each blank-delimited word, one at a time:

begin %x is string len 64 initial('I am the Eggman! I am the Walrus! Koo Koo Kachoo!') repeat while %x:length print %x:before(' ') %x = %x:after(' ') end repeat end

See also