Equivalent of Trim( ) function - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Equivalent of Trim( ) function

sheetalsheetal Posts: 6Member
i would like to know if there is any equivalent of alltrim() function to get the length of a inputbox/textbox excluding the blank/white spaces in the start and end of the textbox.
i get the length of string entered using len() and for loop.
but is there any such function i can use?thanks


  • AKNAKN Posts: 7Member
    Javascript has no direct function for Trimming the string. but you can function for
    trim. below is an example for trimming leading and trailing spaces of a string.

    *source from MSDN

    function trim_string() {
    var ichar, icount;
    var strValue = this;
    ichar = strValue.length - 1;
    icount = -1;
    while (strValue.charAt(ichar)==' ' && ichar > icount)
    if (ichar!=(strValue.length-1))
    strValue = strValue.slice(0,ichar+1);
    ichar = 0;
    icount = strValue.length - 1;
    while (strValue.charAt(ichar)==' ' && ichar < icount)
    if (ichar!=0)
    strValue = strValue.slice(ichar,strValue.length);
    return strValue;

    var st,st1
    st = " Hello good morning "
    st1 = st.trim_string() //returns the value 'hello good Morning'

Sign In or Register to comment.