Chapter: Internet & World Wide Web HOW TO PROGRAM - Wireless Internet and m-Business

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

String Object Methods

Characters are the fundamental building blocks of WMLScript programs. Every program is composed of a sequence of characters that, when grouped together meaningfully, are interpreted by the wireless device as a series of instructions used to accomplish a task.

String Object Methods

 

Characters are the fundamental building blocks of WMLScript programs. Every program is composed of a sequence of characters that, when grouped together meaningfully, are interpreted by the wireless device as a series of instructions used to accomplish a task.

 

The String object provides methods for selecting characters, obtaining substrings and searching for substrings within a string. A complete list of String object methods is available at

 

www.wirelessdevnet.com/channels/

coderef.phtml?catid=5&subid=5.

 

Figure 23.14 demonstrates some of these methods.

 

            <?xml version = "1.0"?>

            <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN"

            "http://www.wapforum.org/DTD/wml12.dtd">

           

            <!-- Fig. 23.11: fig23_11.wml -->

            <!-- Squaring numbers     -->

                       

            <wml>

            <card id = "index" title = "Number Squared">

           

            <!-- soft key to invoke function count -->

            <do type = "accept" label = "OK">

            <go href = "squareNumbers.wmls#count()" />

            </do>

 

            <p>

            Press OK to square a number.

            </p>

            </card>

 

            <card id = "result" title = "Results">

 

            <!-- soft key that returns the user to the previous card -->

            <do type = "accept" label = "Home">

            <prev />

            </do>

<p>

      $result1

      </p>

      </card>

</wml>


Fig. 23.11 Squaring a number by using programmer-defined functions.

(Image of UP.SDK courtesy Openwave Systems Inc. Openwave, the Openwave logo, and UP.SDK are trademarks of Openwave Systems Inc. All rights reserved.)

 

 

    // Fig. 23.12: getVariable.wmls

// Using the WMLBrowser object’s getVar method

    extern function getName()

    {

    var x = WMLBrowser.getVar( "username" );

    var y = x + ", thanks for visiting!";

8

    WMLBrowser.setVar( "result", y );

      WMLBrowser.go( "#card2" );

}

Fig. 23.12  Using the WMLBrowser object’s getVar method

 

            <?xml version = "1.0"?>

            <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN"

            "http://www.wapforum.org/DTD/wml12.dtd">

                       

            <!-- Fig. 23.13: getVar.wml         -->

            <!-- Using the WMLBrowser object’s getVar method -->

                       

            <wml>

            <card id = "index" title = "getVar">

            <do type = "accept" label = "Run">

           

<!-- call function getName -->

            <go href = "getVariable.wmls#getName()" />

           

            </do>

           

            <p>

            Enter your name:<br />

           

            <!-- create input box for user input -->

            <input name = "username" value = "" />

           

            </p>

            </card>

           

            <card id = "card2" title = "getVar">

                                               

            <!--     create            a soft key to return the client -->

            <!--     to the previous card          -->

            <do type = "accept" label            = "Back">

            <prev />

            </do>

 

            <p>

            $result <!-- dereference browser variable result -->

            </p>

            </card>

            </wml>


Fig. 23.13 Setting and displaying a variable using WMLScript. (Image of UP.SDK courtesy Openwave Systems Inc. Openwave, the Openwave logo, and UP.SDK are trademarks of Openwave Systems Inc. All rights reserved.)

 

The example begins with the value of variable string1 set to an empty string (""). Through the course of the example, we call String object methods to change the string

"Wireless Web" to the string "Deitel Book".

 

    // Fig. 23.14: functionSet.wmls

    // Demonstrating String object methods

 

    extern function stringMethods()

    {

    var string1 = "";

var empty;

 

    // test if string1 is empty

      if ( String.isEmpty( string1 ) )

      empty = "string1 is empty";

      else

      empty = "string1 is not empty";

 

          WMLBrowser.setVar( "emptyString1", empty );

 

      // format the string to have 12 spaces between

      // "Wireless" and "Web"

      string1 = String.format( "Wireless%15s", "Web" );

 

          WMLBrowser.setVar( "formatString1", string1 );

 

      // squeeze the string until one space is left

      string1 = String.squeeze( string1 );

          WMLBrowser.setVar( "squeezeString1", string1 );

 

      // use method element to count the elements in string1

      // use the toString method to convert the integer to

      // a string

      var count = String.toString( String.elements( string1, " ") );

 

          WMLBrowser.setVar( "elementsString1", count );

      // find string starting at index 8 and ending with a space

      var string1Element = String.elementAt( string1, 8, " " );

 

          WMLBrowser.setVar( "elementAtString1", string1Element );

 

      // get the length of string1

      var length = String.length( string1 );

 

      // insert "Book" at the end of string1

      string1 = String.insertAt( string1, "Book", length, " " );

 

          WMLBrowser.setVar( "insertAtString1", string1 );

 

      // replace "Web" with "Deitel" where "Web" has an

      // index of 1

      string1 = String.replaceAt( string1, "Deitel", 1, " " );

 

          WMLBrowser.setVar( "replaceAtString1", string1 );

      // remove "Wireless" from string1

      string1 = String.removeAt( string1, 0, " " );

 

      WMLBrowser.setVar( "removeAtString1", string1 );

      WMLBrowser.go( "#card2" );

}

 

Fig. 23.14  Demonstrating String object methods

 

 

In line 6, we declare variable string1 and assign it to an empty string. Line 10 deter-mines if the value of string1 is empty. If the condition in line 10 evaluates to true, vari-able empty is assigned the string "string1 is empty"; otherwise, empty is assigned the string “string1 is not empty”. Line 15 calls WMLBrowser method setVar to declare browser variable emptystring1, setting its value to that of variable empty. We assign each value to a different browser variable throughout the script so that the results in Fig. 23.15 are displayed.

 

In line 19, we assign to string1 the strings "Wireless" and "Web". By calling method format, we place 12 spaces (indicated by %15s) between the two strings. Although the notation %15s creates 15 space characters, three are occupied by the string “Web”, leaving only 12 spaces between the two strings. The Openwave browser automat-ically wraps the line (Fig. 23.15) because it is too long to display on a single line. The value of string1 becomes

 

"Wireless            Web"

 

Line 24 calls String method squeeze to combine all consecutive whitespace char-acters in string1 into a single whitespace character. Variable string1 now becomes

 

"Wireless Web"

 

Line 31 calls String method elements to return the number of words in the string, separated by a space character. Method toString converts the integer value returned by elements to a string. This method is useful if a value must be concatenated to another string.

In line 36, we call method elementAt to return a substring of string1 from index 8 to the first space encountered. Indices point to individual characters in a string, beginning at 0. In this case, the string returned is

 

"Web"

 

In line 41, we call method length to retrieve the number of characters in string1. This value is assigned to variable length. In line 44, we call method insertAt to insert the string "Book" into the end of string1, separated by a space. The value of string1 becomes

 

"Wireless Web Book"

 

Line 50 calls String method replaceAt to return a new string in which the string "Deitel" replaces everything from the first space to the second space in string1. After the replaceAt method is invoked, the value of string1 becomes

 

"Wireless Deitel Book"

 

Line 55 calls method removeAt to remove characters in string1 from index 0 to the first occurrence of a space. When invoked, method removeAt removes the string "Wireless". The value of string1 becomes

 

"Deitel Book"

 

 

The WML document in Fig 23.15 calls function stringMethods in Fig. 23.14 and displays the results.

 

 

            <?xml version = "1.0"?>

            <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN"

            "http://www.wapforum.org/DTD/wml12.dtd">

                       

            <!-- Fig. 23.15: stringMisc.wml  -->

            <!-- WML document that references stringMisc.wmls -->

                       

            <wml>          

                       

            <card id = "index" title = "strings">

            <do type = "accept" label = "Run">

           

            <!-- call function stringMethods in functionSet.wmls -->

            <go href = "functionSet.wmls#stringMethods()" />

           

            </do>

           

            <p>

            Click Run to execute the script.

            </p>

            </card>

 

            <card id  ="card2" title = "strings">

            <do type = "accept" label = "Run">

           

 

<!-- redirect the user to the next card to -->

            <!-- display further results           -->

            <go href = "#card3" />    

                       

            </do>           

                       

            <p>    

            isEmpty method:<br />   

                       

            <!-- dereference browser variable emptyString1 -->

            $emptyString1       

                       

            </p>

            </card>

           

            <card id = "card3" title = "strings">

 

            <do type = "accept" label = "Run">

 

<!-- redirect the user to the next card to --> <!-- display further results --> <go href = "#card4" />

 

                                   

            </do>           

                                   

            <p>                

                        String.format method:<br />    

                                   

                        <!-- dereference browser variable formatString1 -->        

            $formatString1

            </p>

            </card>

 

            <card id = "card4" title = "strings">

            <do type = "accept" label = "Run">

           

            <!--     redirect the user to the next card to -->

            <!--     display further results       -->

            <go href = "#card5" />

            </do>

           

            <p>

            String.squeeze method:<br />

           

            <!-- dereference browser variable squeezeString1 -->

            $squeezeString1

            </p>

 

            </card>

 

 

            <card id = "card5" title = "strings">

            <do type = "accept" label = "Run">

           

            <!--     redirect the user to the next card to -->

            <!--     display further results       -->

            <go href = "#card6" />

            </do>

           

            <p>

            String.elements method:<br />

           

            <!-- dereference browser variable elementsString1 -->

            $elementsString1

            </p>

            </card>

 

            <card id = "card6" title = "strings">

            <do type = "accept" label = "Run">

           

            <!--     redirect the user     to the next card to -->

            <!--     display further results       -->

            <go href = "#card7" />                

            </do>           

                       

            <p>    

            String.elementAt method:<br />          

                       

            <!-- dereference browser variable elementAtString1 -->

            $elementAtString1

            </p>

            </card>

 

            <card id = "card7" title = "strings">

            <do type = "accept" label = "Run">

           

            <!--     redirect the user to the next card to -->

            <!--     display further results       -->

            <go href = "#card8" />

            </do>

 

            <p>

            String.insertAt method:<br />

           

            <!-- dereference browser variable insertAtString1 -->

            $insertAtString1

            </p>

            </card>

 

            <card id = "card8" title = "strings">

            <do type = "accept" label = "Run">

           

            <!--     redirect the user to the next card to -->

            <!--     display further results       -->

            <go href = "#card9" />

            </do>

 

            <p>

            String.replaceAt method:<br />

           

            <!-- dereference browser variable replaceAtString1 -->

            $replaceAtString1

            </p>

            </card>

 

            <card id = "card9" title = "strings">

            <do type = "accept" label = "Home">

           

            <!-- redirect the user to the first card -->

            <go href = "#index" />

            </do>

 

            <p>

 

String.removeAt method:<br />

 

 

            <!-- dereference browser variable removeAtString1 -->

            $removeAtString1

            </p>

            </card>

            </wml>

 

 


 

Fig. 23.15 WML document that references stringMisc.wmls.. (Image of UP.SDK courtesy Openwave Systems Inc. Openwave, the Openwave logo, and UP.SDK are trademarks of Openwave Systems Inc. All rights reserved.)

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.