Javascript string functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TNT
    New Member
    • Feb 2007
    • 48

    Javascript string functions

    I have two questions concerning Javascript

    1: Is there a way to search for a character in a string?
    2: Is there a way to "chop off" some characters from the start of a string?
  • iam_clint
    Recognized Expert Top Contributor
    • Jul 2006
    • 1207

    #2
    yes
    [CODE=javascript]
    var string = "the cat"
    string.indexOf( "a");
    [/CODE]
    will return 5 because the a is the 5th character

    You can read this Article on string functionality.

    and yes you can chop off characters.
    heres some left and right functions you may be used to using in other languages.
    [CODE=javascript]
    function Left(str, n){
    if (n <= 0)
    return "";
    else if (n > String(str).len gth)
    return str;
    else
    return String(str).sub string(0,n);
    }
    function Right(str, n){
    if (n <= 0)
    return "";
    else if (n > String(str).len gth)
    return str;
    else {
    var iLen = String(str).len gth;
    return String(str).sub string(iLen, iLen - n);
    }
    }
    [/CODE]

    Comment

    • TNT
      New Member
      • Feb 2007
      • 48

      #3
      Originally posted by iam_clint
      yes
      [CODE=javascript]
      var string = "the cat"
      string.indexOf( "a");
      [/CODE]
      will return 5 because the a is the 5th character

      You can read this Article on string functionality.

      and yes you can chop off characters.
      heres some left and right functions you may be used to using in other languages.
      [CODE=javascript]
      function Left(str, n){
      if (n <= 0)
      return "";
      else if (n > String(str).len gth)
      return str;
      else
      return String(str).sub string(0,n);
      }
      function Right(str, n){
      if (n <= 0)
      return "";
      else if (n > String(str).len gth)
      return str;
      else {
      var iLen = String(str).len gth;
      return String(str).sub string(iLen, iLen - n);
      }
      }
      [/CODE]
      I am new to Javascript (only reading a book on the basics four days ago)
      Can you post examples instead of the "str" and "n"?

      Comment

      • iam_clint
        Recognized Expert Top Contributor
        • Jul 2006
        • 1207

        #4
        ok so you have these functions
        [CODE=javascript]
        function Left(str, n){
        if (n <= 0)
        return "";
        else if (n > String(str).len gth)
        return str;
        else
        return String(str).sub string(0,n);
        }
        function Right(str, n){
        if (n <= 0)
        return "";
        else if (n > String(str).len gth)
        return str;
        else {
        var iLen = String(str).len gth;
        return String(str).sub string(iLen, iLen - n);
        }
        }
        [/CODE]

        now you want the right 4 characters of a string

        [CODE=javascript]
        var thestring = "this is a simple test";
        var somestring = Right(thestring , 4);
        alert(somestrin g);
        [/CODE]

        somestring would = test

        [CODE=javascript]
        var thestring = "this is a simple test";
        var somestring = Left(thestring, 4);
        alert(somestrin g);
        [/CODE]

        somestring would = this

        Comment

        • markhu
          New Member
          • Aug 2009
          • 2

          #5
          the real way to do string slices

          There is no leftstr() or rightstr() functions in JavaScript because the slice() function handles both.

          Here is an example that shows the first 4 chars of a string, then the last 4.

          Code:
          x="sample string";
          
          r=x.slice(0,4) + ' : ' + x + ' : ' + x.slice(-4);
          
          alert( r );
          r will be "samp : sample string : ring"

          Comment

          Working...