JS常用方法--String.prototype使用!

JS常用方法--String.prototype使用!

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
    <title>JS常用方法--String.prototype使用!  </title>
    <meta name="Generator" content="EditPlus">
    <meta name="Author" content="">
    <meta name="Keywords" content="">
    <meta name="Description" content="">
    <script language="javascript">
String.prototype.Replace = function(oldValue,newValue)
    {
        var reg = new RegExp(oldValue,"g");
        return this.replace(reg, newValue);
    }
    //字符串替换;曾经很头疼写了很多代码,还是这个简单
    function replace(obj)
    {
        alert(obj.value.Replace("a","d"));
    }
    // 另存为文件
    function SaveCode(obj, filename)
    {
        var win = window.open('', '_blank', 'top=100');
        var code = obj.innerText;
        code = code == null || code == "" ? obj.value : code;
        win.opener = null;
        win.document.write(code);
        win.document.execCommand('saveas', true, filename);
        win.close();
    }
    // 问候
    window.onload = function()
    {   
        var now = new Date();
        var hour = now.getHours();
        var greeting;
        if (hour < 6)
            greeting = "凌晨好";
        else if (hour < 10)
            greeting = "早上好";
        else if (hour < 14)
            greeting = "中午好";
        else if (hour < 18)
            greeting = "下午好";
        else
            greeting = "晚上好";
           
        document.getElementById("hi").innerHTML = "<font color=red>" + greeting + "</font>" ;
    }
    // 将光标停在对象的最后
    function PutCursorAtLast(obj)
    {  
        obj.focus();
        var range = obj.createTextRange();
        range.moveStart('character',obj.value.length);
        range.collapse(true);
        range.select();
    }
    // 将光标停在对象的最前
    function PutCursorAtFirst(obj)
    {  
        obj.focus();
        var range = obj.createTextRange();
        range.moveStart('character',0);
        range.collapse(true);
        range.select();
    }
</script>
</head>

<body>

 

<span id="hi"></span><br />


<span>curssor at last </span><br />
<input type="text" value="curssor at last" οnclick="PutCursorAtLast(this)">
<br />


<span>curssor at first </span><br />
<input type="text" value="curssor at first" οnclick="PutCursorAtFirst(this)">
<br />


<span>String.Replace </span><br />
<input type="TEXT" value="replace" οnclick="replace(this)">
<br />


<span>save file </span><br />
<input type="text" value="hello word" οnclick='SaveCode(this,"save")' /><br/>

 

<script type='text/javascript'>
// 返回字符的长度,一个中文算2个
String.prototype.ChineseLength=function()
{
    return this.replace(/[^/x00-/xff]/g,"**").length;
}
// 判断字符串是否以指定的字符串结束
String.prototype.EndsWith = function(str)
{
    return this.substr(this.length - str.length) == str;
}
// 去掉字符左端的的空白字符
String.prototype.LeftTrim = function()
{
    return this.replace(/(^[//s]*)/g, "");
}
// 去掉字符右端的空白字符
String.prototype.RightTrim = function()
{
    return this.replace(/([//s]*$)/g, "");
}
// 判断字符串是否以指定的字符串开始
String.prototype.StartsWith = function(str)
{
    return this.substr(0, str.length) == str;
}
// 去掉字符两端的空白字符
String.prototype.Trim = function()
{
    return this.replace(/(^/s*)|(/s*$)/g, "");
}
</script>

<input type="text" id="txtone" value="例子test"/><input type='button' value="返回字符的长度,一个中文算2个" οnclick="alert(document.getElementById('txtone').value.ChineseLength())" /><br/>


<input type="text" id="txttwo" value="例子test"/> 结尾字符串:<input type="text" id="txttwo1" value="test"/>
<input type='button' value="判断字符串是否以指定的字符串结束" οnclick="alert(document.getElementById('txttwo').value.EndsWith(document.getElementById('txttwo1').value))" /><br/>


<input type="text" id="txtthree" value=" 例子test "/><input type='button' value="去掉字符左端的的空白字符" οnclick="document.getElementById('txtthree').value = document.getElementById('txtthree').value.LeftTrim()" /><br/>


<input type="text" id="txtfour" value=" 例子test "/><input type='button' value="去掉字符右端的空白字符" οnclick="document.getElementById('txtfour').value = document.getElementById('txtfour').value.LeftTrim()" /><br/>


<input type='text' type="text" id="txtfive" value=" sstest "/> 开始字符串:<input type="text" id="txtfive1" value="ss"/>
<input type='button' value="判断字符串是否以指定的字符串开始" οnclick="alert(document.getElementById('txtfive').value.StartsWith(document.getElementById('txtfive1').value))" /><br/>


<input type="text" id="txtsix" value=" 例子test "/><input type='button' value="去掉字符两端的空白字符" οnclick="document.getElementById('txtsix').value = document.getElementById('txtsix').value.Trim();" /><br/>


</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值