Prototype实战教程----16.string


 本文示例源代码或素材下载


<html>
<head>
<title>String Test</title>
<script src="prototype.js"></script>
<script>
function testStrip()
{
  var str=new String('    wanfangdata   ');
  alert(str.strip()); 
  }
  function teststripTags()
{
  var str=new String('<html>2002<head>2005<body> ');
  alert(str.stripTags()); 
  }
  function testtruncate()
{
  var str=new String('king123456789987654321');
  alert(str.truncate(8,'*')); 
  }
  
  function testcamelize()
  {
    var str=new String('Background-color')
    alert(str.camelize())
    }
    function testcapitalize()
  {
      var str=new String('back');
  alert(str.capitalize());
    }
    
function testDasherize()
{
  var str=new String('Hello_World');
  alert(str.dasherize()); 
  }

function testinspect()
{
  alert("sdfj"sfa".inspect() );
  }
  function testgsub()
  {
    alert("skdjfAsfdjkAdk".gsub(/A/,function(match) {return match[0].toLowerCase()}));
    }
    function testScan()
    {
      alert("skdjfAsfdjkAdk".scan(/A/,function(){alert('have a A')}));
      }
  function testUnderscore()
  {alert('borderBottomWidth'.underscore());

 }
    function testTemplate()
    {var temp = new Template("被替换");
      
      alert(temp.evaluate('aAklkljkjAkka'));
      }
      function testTimes()
    {
      
      alert('['+'Q'.times(10)+']');
      }
      function testEval()
      {
        var str="<script"+">alert('ok');<"+"/script>";
        str.evalScripts();
        }
</script>
<body>
<p><input type=button value='Strip' onclick='testStrip();' />"    wanfangdata     " ---> 'wanfangdata'去掉两边的空格</p><br/>
<p><input type=button value='stripTags' onclick='teststripTags();' />“&lt;html&gt;2002&lt;head&gt;2005&lt;body&gt;” ---> '20022005'去掉html标签</p><br/>
<p><input type=button value='truncate' onclick='testtruncate();' />“123456789>987654321” ---> '123456>'字符串截断</p><br/>
<p><input type=button value='camelize' onclick='testcamelize();' />Background-color --->BackgroundColor</p><br/>
<p><input type=button value='capitalize' onclick='testcapitalize();' />back--->Back</p><br/>
<p><input type=button value='inspect' onclick='testinspect();' />sdfj"sfa--->sdfj"sfa</p><br/>
<p><input type=button value='testgsub' onclick='testgsub();' />skdjfAsfdjkAdk--->skdjfasfdjkadk</p><br/>
<p><input type=button value='testScan' onclick='testScan();' />字符串检测</p><br/>
<p><input type=button value='testTemplate()' onclick='testTemplate();' />字符串模板</p><br/>
<p><input type=button value='testTimes' onclick='testTimes();' />字符串重复</p><br/>
<p><input type=button value='执行脚本' onclick='return testEval();' />执行字符串脚本</p><br/>
<p><input type=button value='testUnderscore' onclick='testUnderscore();' />'borderBottomWidth’.underscore() -> 'border_bottom_width’
</p><br/>
<p><input type=button value='testDasherize' onclick='testDasherize();' />'Hello_World' ---> 'Hello-World'把下划线转化成-</p><br/>
</body>
</html>

本文作者:
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3