Convert from c# function to javascript function
- by socheata
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.