Convert from c# function to javascript function

Posted by socheata on Stack Overflow See other posts from Stack Overflow or by socheata
Published on 2012-10-01T03:28:15Z Indexed on 2012/10/01 3:37 UTC
Read the original article Hit count: 122

Filed under:
|

I have a function in c# that used to manipulate the string, It works well while I used in C#. Now I want to convert this function to use in javascript. This is the function in c#:

 public static string TrimString(string str, int lenght)
 {
        string _str = str;

        int _iAdditionalLenght = 0;

        for (int i = lenght; i < str.Length; i++)
        {
            if (_str.Substring(i, 1) == " ")
                break;

            _iAdditionalLenght++;
        }

        return str.Substring(0, str.Length < (lenght + _iAdditionalLenght) ? str.Length : (lenght + _iAdditionalLenght));

 }

I converted it to javascript :

function TrimString(str, lengthStr) { //this is my testing 4
     var _str = str;
     var _iAdditionalLenght = 0;
     for (var i = lengthStr; i < str.length; i++) {
       if (_str.substring(i, 1) == " ")
           break;
       _iAdditionalLenght++;
     }

     return str.substring(0, str.length < (lengthStr + _iAdditionalLenght) ? str.length : (lengthStr + _iAdditionalLenght));
 }

But the javascript doesn't work.

Could anyone tell me, how could I do it in javascript function?

Thanks you so much.

© Stack Overflow or respective owner

Related posts about c#

Related posts about JavaScript