简体   繁体   English

如何修剪JavaScript中的字符串?

[英]how to trim string in javascript?

Hi I am trying to trim string from both end in javascript but it not work properly.Script can't work or some times it lost focus from textbox. 嗨,我正在尝试在javascript中从两端修剪字符串,但是它无法正常工作。脚本无法正常工作,或者有时它失去了文本框的焦点。 I am write javascript like below & calling it in validate function 我正在写如下的javascript并在validate函数中调用它

function trim(s) 
        {
            if (typeof s!= "string") {
                return s;
             }
             var retValue = s;
             var ch = retValue.s(0, 1);
             while (ch == " ")
             {      retValue = retValue.substring(1, retValue.length);
                    ch = retValue.substring(0, 1);
             }
             ch = retValue.substring(retValue.length-1, retValue.length);
             while (ch == " ")
             {
                 retValue = retValue.substring(0, retValue.length-1);
                 ch = retValue.substring(retValue.length-1, retValue.length);
            }
            while (retValue.indexOf("  ") != -1)
            {
                retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length);
            }
                return retValue; 
        } 

       function validate() {
        // alert("Please! Enter  Farm Name");
        if (!trim(document.getElementById("<%=txtFarm_Name.ClientID%>").value)) {
            alert("Please! Enter  Farm Name");
            document.getElementById("<%=txtFarm_Name.ClientID%>").focus();
            return false;
        }

} }

You could do some thing like the following 您可以执行以下操作

if(!String.prototype.trim) {
  String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g,'');
  };
}

Once the above code is executed it can be use like the following 一旦执行了以上代码,就可以像下面这样使用

var str = " Hello world  ";
console.log(str.trim());

Or if jquery is being used in the project then something like the following will work too 或者,如果项目中使用了jQuery,那么类似以下的内容也将起作用

var str = " Hello world  ";
$.trim(str);

I had written this function for trim, when the .trim() function was not available in JS way back in 2008. Some of the older browsers still do not support the .trim() function and i hope this function may help you. 当.trim()函数在2008年以JS方式不可用时,我已经编写了此函数进行修整。某些较旧的浏览器仍不支持.trim()函数,希望此函数对您有所帮助。

TRIM FUNCTION 修剪功能

function trim(str)
{
    var startpatt = /^\s/;
    var endpatt = /\s$/;

    while(str.search(startpatt) == 0)
        str = str.substring(1, str.length);

    while(str.search(endpatt) == str.length-1)
        str = str.substring(0, str.length-1);   

    return str;
}

Explanation : The function trim() accept a string object and remove any starting and trailing whitespaces (spaces,tabs and newlines) and return the trimmed string. 说明 :函数trim()接受一个字符串对象,并删除任何开头和结尾的空格(空格,制表符和换行符)并返回修剪后的字符串。 You can use this function to trim form inputs to ensure valid data to be sent. 您可以使用此功能修剪表单输入,以确保发送有效数据。

The function can be called in the following manner as an example. 例如,可以以下方式调用该函数。

form.elements[i].value = trim(form.elements[i].value);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM