var a="slkdfgldsklgksdl";
var b=a.substr(0,5);

解决方案 »

  1.   

    function String.prototype.right(n){
    if (isNaN(n)) throw 0;
    n=parseInt(n);
    if (n>=this.length) return this;
    if (n<=0) return ""
    return this.substr(this.length-n)
    }
    a="slkdfgldsklgksdl";
    b=a.right(5);
    alert(b);
      

  2.   

    a1=a.substr(a.length-4,up_file.length);
    能取到值吗?
      

  3.   

    substring 才能指定开始结束位置
    substr 只能指定开始位置,自动截断到串末尾
      

  4.   

    看你up_file.length返回的值是否>0了.
    Syntax
    stringvar.substr(start [, length ]) 
    The substr method syntax has these parts: Part Description 
    stringvar Required. A string literal or String object from which the substring is extracted. 
    start Required. The starting position of the desired substring. The index of the first character in the string is zero. 
    length Optional. The number of characters to include in the returned substring. 
    Res
    If length is zero or negative, an empty string is returned. If not specified, the substring continues to the end of stringvar.
    The following example illustrates the use of the substr method:
    function SubstrDemo()
    {
      var s, ss;
      var s = "The quick brown fox jumped over the lazy yellow dog.";
      ss = s.substr(16, 3);
      // Returns "fox"
      return(ss);
    }