JavaScript Tutorial/Dojo toolkit/String

Материал из Web эксперт
Версия от 18:52, 25 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

dojo.string.pad("0", 5, "1")

<html>
  <head>
    <link rel="StyleSheet" type="text/css"
      href="js/dojo/dijit/themes/tundra/tundra.css">
    <script type="text/javascript">
      var djConfig = {
        baseScriptUri : "js/dojo/",
        parseOnLoad : true
      };
    </script>
    <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
    <script>
      dojo.require("dojo.parser");
      dojo.require("dojo.string");
      dojo.addOnLoad(f);
      
      function f(){
        alert(dojo.string.pad("0", 5, "1"));
      }
    </script>
  </head>
  <body class="tundra">
     
</body>
</html>


dojo.string.pad("0", 5, "1", true)

<html>
  <head>
    <link rel="StyleSheet" type="text/css"
      href="js/dojo/dijit/themes/tundra/tundra.css">
    <script type="text/javascript">
      var djConfig = {
        baseScriptUri : "js/dojo/",
        parseOnLoad : true
      };
    </script>
    <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
    <script>
      dojo.require("dojo.parser");
      dojo.require("dojo.string");
      dojo.addOnLoad(f);
      
      function f(){
        alert(dojo.string.pad("0", 5, "1", true));
      }
    </script>
  </head>
  <body class="tundra">
     
</body>
</html>


dojo.string.pad("", 5)

<html>
  <head>
    <link rel="StyleSheet" type="text/css"
      href="js/dojo/dijit/themes/tundra/tundra.css">
    <script type="text/javascript">
      var djConfig = {
        baseScriptUri : "js/dojo/",
        parseOnLoad : true
      };
    </script>
    <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
    <script>
      dojo.require("dojo.parser");
      dojo.require("dojo.string");
      dojo.addOnLoad(f);
      
      function f(){
        alert(dojo.string.pad("", 5));
      }
    </script>
  </head>
  <body class="tundra">
     
</body>
</html>


dojo.string.pad("", 5, " ") space

<html>
  <head>
    <link rel="StyleSheet" type="text/css"
      href="js/dojo/dijit/themes/tundra/tundra.css">
    <script type="text/javascript">
      var djConfig = {
        baseScriptUri : "js/dojo/",
        parseOnLoad : true
      };
    </script>
    <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
    <script>
      dojo.require("dojo.parser");
      dojo.require("dojo.string");
      dojo.addOnLoad(f);
      
      function f(){
        alert(">"+dojo.string.pad("", 5, " ")+"<");
      }
    </script>
  </head>
  <body class="tundra">
     
</body>
</html>


dojo.string.substitute

<html>
  <head>
    <script type="text/javascript">
      var djConfig = {
        baseScriptUri : "js/dojo/"
      };
    </script>
    <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
    <script language="JavaScript" type="text/javascript">
      dojo.require("dojo.string");
      var template = "${0}!  Have a nice ${1}!";
      var tokenValues = [ "A", "weekend" ];
      alert(dojo.string.substitute(template, tokenValues));
    </script>
  </head>
  <body></body>
</html>


dojo.string.substitute("${0} and ${1}.", ["Jack", "Jill"])

<html>
  <head>
    <link rel="StyleSheet" type="text/css"
      href="js/dojo/dijit/themes/tundra/tundra.css">
    <script type="text/javascript">
      var djConfig = {
        baseScriptUri : "js/dojo/",
        parseOnLoad : true
      };
    </script>
    <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
    <script>
      dojo.require("dojo.parser");
      dojo.require("dojo.string");
      dojo.addOnLoad(f);
      
      function f(){
        alert(dojo.string.substitute("${0} and ${1}.", ["Jack", "Jill"]));
      }
    </script>
  </head>
  <body class="tundra">
     
</body>
</html>


Is it a string

<html>
  <head>
    <script type="text/javascript">
      var djConfig = {
        baseScriptUri : "js/dojo/"
      };
    </script>
    <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
    <script>
      var s1 = new String("I am a string");
      var s2 = "a string";
      var s3 = 100;
      alert(dojo.isString(s1) + ", " + dojo.isString(s2) + ", " +
        dojo.isString(s3));
    </script>
  </head>
  <body></body>
</html>


Padding string

<html>
  <head>
    <script type="text/javascript">
      var djConfig = {
        baseScriptUri : "js/dojo/"
      };
    </script>
    <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
    <script language="JavaScript" type="text/javascript">
      dojo.require("dojo.string");
      alert(dojo.string.pad("wbex.ru", 20, "*"));
    </script>
  </head>
  <body></body>
</html>


Parse string as number

<html>
  <head>
    <script type="text/javascript">
      var djConfig = {
        baseScriptUri : "js/dojo/"
      };
    </script>
    <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
    <script language="JavaScript" type="text/javascript">
      dojo.require("dojo.number");
      alert((dojo.number.parse("1234.56") + 1));
    </script>
  </head>
  <body></body>
</html>


Remove letter from string

<html>
  <head>
    <script type="text/javascript">
      var djConfig = {
        baseScriptUri : "js/dojo/",
        parseOnLoad : true
      };
    </script>
    <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
    <script>
      dojo.require("dojox.string.Builder");
      dojo.require("dojox.string.sprintf");
      function testIt() {
        var b = new dojox.string.Builder("Java");
        alert(b);
        b.remove(0, 1);
        alert(b);
      }
    </script>
  </head>
  <body onLoad="testIt();">
  </body>
</html>


substitute with function

<html>
  <head>
    <link rel="StyleSheet" type="text/css"
      href="js/dojo/dijit/themes/tundra/tundra.css">
    <script type="text/javascript">
      var djConfig = {
        baseScriptUri : "js/dojo/",
        parseOnLoad : true
      };
    </script>
    <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
    <script>
      dojo.require("dojo.parser");
      dojo.require("dojo.string");
      dojo.addOnLoad(f);
      
      function f(){
        alert(dojo.string.substitute("${0} and ${1}.", ["A", "B"],function(x) {return "("+x+")";}));
      }
    </script>
  </head>
  <body class="tundra">
     
</body>
</html>


substitute with named parameters

<html>
  <head>
    <link rel="StyleSheet" type="text/css"
      href="js/dojo/dijit/themes/tundra/tundra.css">
    <script type="text/javascript">
      var djConfig = {
        baseScriptUri : "js/dojo/",
        parseOnLoad : true
      };
    </script>
    <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
    <script>
      dojo.require("dojo.parser");
      dojo.require("dojo.string");
      dojo.addOnLoad(f);
      
      function f(){
        alert(dojo.string.substitute("${person1} and ${person2}.", {person1: "J", person2: "J"}));
      }
    </script>
  </head>
  <body class="tundra">
     
</body>
</html>


Trim a string

<html>
  <head>
    <script type="text/javascript">
      var djConfig = {
        baseScriptUri : "js/dojo/"
      };
    </script>
    <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
    <script language="JavaScript" type="text/javascript">
      dojo.require("dojo.string");
      var s = "   A";
      if (dojo.string.trim(s) == "A") {
        alert("Matched with trim");
      }
    </script>
  </head>
  <body></body>
</html>