SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    Xbox why have you forsaken me? moospot's Avatar
    Join Date
    Feb 2001
    Location
    Clearwater, FL
    Posts
    3,615
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I Truncate Strings?

    I can't seem to find the answer. In ASP you can use the Left, Right and Mid functions. But how do you do it in Javascript?

  2. #2
    morphine for a wooden leg randem's Avatar
    Join Date
    Jun 2002
    Location
    .chicago.il.us
    Posts
    957
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: How do I Truncate Strings?

    Originally posted by moospot
    I can't seem to find the answer. In ASP you can use the Left, Right and Mid functions. But how do you do it in Javascript?
    var a = " a string. ";
    a = a.replace(/(^\s+)(\s+$)/g, "");
    ----Adopt-a-Sig----
    Your message here!

  3. #3
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the substring method for Strings works pretty well for that...

    var myStr = "This is a string";
    var one = myStr.substring(5);
    var two = myStr.substring(3,10);
    var three = myStr.substring(0,myStr.length-5);

    // one == "is a string"
    // two == "s is a "
    // three == "This is a s"
    Last edited by beetle; Oct 28, 2002 at 09:47.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  4. #4
    Xbox why have you forsaken me? moospot's Avatar
    Join Date
    Feb 2001
    Location
    Clearwater, FL
    Posts
    3,615
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hm. The substring looks like what I'm after. The other one looks like a regular expression.

    Thanks, Beetle.

  5. #5
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ya, randem's regular expression removes trailing and leading whitespace from a string....
    Code:
    var myStr = "     a string     ";
    myStr = myStr.replace(/(^\s+)(\s+$)/g, "");
    // myStr == "a string";
    Handy, but he probably misundertsood exactly what you were looking for.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •