How 2 get a substring from a string?

Discussion in 'Javascript' started by Jean-Guy Mouton, Sep 24, 2007.

  1. Hello,

    I am teaching myself Dom scripting with Javascript. I do not understand
    how to get a substring. If I have strings in the form:
    "This is string number 1"
    How can I put "1" into a variable to use it elsewhere? I thought I
    could use the function split and create an array but I have different
    string lengths with a variable number of words...
    Can you please help me? Thank you.
     
    Jean-Guy Mouton, Sep 24, 2007
    #1
    1. Advertising

  2. Jean-Guy Mouton wrote:
    > I am teaching myself Dom scripting with Javascript. I do not understand
    > how to get a substring. If I have strings in the form: "This is string
    > number 1" How can I put "1" into a variable to use it elsewhere?


    var one = s.match(/\d+/)[0];

    > I thought I could use the function split and create an array but I have
    > different string lengths with a variable number of words...


    So?

    var
    a = s.split(/\s/),
    lastWord = a[a.length - 1];


    PointedEars
    --
    realism: HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness: XHTML 1.1 as application/xhtml+xml
    -- Bjoern Hoehrmann
     
    Thomas 'PointedEars' Lahn, Sep 24, 2007
    #2
    1. Advertising

  3. Thomas 'PointedEars' Lahn wrote:
    > Jean-Guy Mouton wrote:
    >> I thought I could use the function split and create an array but I have
    >> different string lengths with a variable number of words...

    >
    > So?
    >
    > var
    > a = s.split(/\s/),


    a = s.split(/\s+/),

    > lastWord = a[a.length - 1];
     
    Thomas 'PointedEars' Lahn, Sep 24, 2007
    #3
  4. Thomas 'PointedEars' Lahn wrote:
    >
    > var
    > a = s.split(/\s/),
    > lastWord = a[a.length - 1];
    >

    Great! Thanks a lot Thomas.
     
    Jean-Guy Mouton, Sep 24, 2007
    #4
  5. In comp.lang.javascript message <46f77242$0$7330$>,
    Mon, 24 Sep 2007 10:15:38, Jean-Guy Mouton <> posted:

    >I am teaching myself Dom scripting with Javascript. I do not
    >understand how to get a substring. If I have strings in the form:
    >"This is string number 1"
    >How can I put "1" into a variable to use it elsewhere? I thought I
    >could use the function split and create an array but I have different
    >string lengths with a variable number of words...
    >Can you please help me? Thank you.



    There are several ways of doing that, depending on how you want to
    define the desired extraction.

    You might want the 23rd character, or all characters after the 22nd, or
    the last character, or the character after the 4th space, or the 5th
    "word", or the first set of consecutive octal digits, or the last set of
    consecutive decimal digits, etc.

    Seek a reference such as <http://developer.mozilla.org/en/docs/Core_Java
    Script_1.5_Reference>, and look for appropriate properties of String,
    such as charAt, match, substring and substr.

    In most cases, at least, one can use a RegExp search; but it may well
    not be necessary,

    Alternatively, you've used the words Javascript and substring - search
    the Web, and you'll soon find references, some being good.

    The reference at sussex.ac.uk, though old, is good; and should appear
    (unlike most) in your favoured font face and size. I frequently use a
    local copy of what it is a copy of.

    It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
    news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr J R Stockton, Sep 24, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    13
    Views:
    2,320
    Robert
    Apr 14, 2005
  2. Nico Grubert
    Replies:
    1
    Views:
    461
    Pierre Quentel
    Jun 21, 2006
  3. =?KOI8-R?Q?=F3=D4=C1=CE=C9=D3=CC=C1=D7_=F1=C7=CC=C

    How to get a substring with variable indices

    =?KOI8-R?Q?=F3=D4=C1=CE=C9=D3=CC=C1=D7_=F1=C7=CC=C, Dec 18, 2006, in forum: Python
    Replies:
    1
    Views:
    353
    Paul Rubin
    Dec 18, 2006
  4. Replies:
    3
    Views:
    230
    Sherm Pendley
    Aug 3, 2005
  5. SM
    Replies:
    4
    Views:
    226
Loading...

Share This Page