Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > How 2 get a substring from a string?

Reply
Thread Tools

How 2 get a substring from a string?

 
 
Jean-Guy Mouton
Guest
Posts: n/a
 
      09-24-2007
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.
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      09-24-2007
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
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      09-24-2007
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];

 
Reply With Quote
 
Jean-Guy Mouton
Guest
Posts: n/a
 
      09-24-2007
Thomas 'PointedEars' Lahn wrote:
>
> var
> a = s.split(/\s/),
> lastWord = a[a.length - 1];
>

Great! Thanks a lot Thomas.
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      09-24-2007
In comp.lang.javascript message <46f77242$0$7330$(E-Mail Removed)>,
Mon, 24 Sep 2007 10:15:38, Jean-Guy Mouton <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to get a substring with variable indices =?KOI8-R?Q?=F3=D4=C1=CE=C9=D3=CC=C1=D7_=F1=C7=CC=CF?= Python 1 12-18-2006 01:30 PM
Search substring in a string and get index of all occurances Nico Grubert Python 1 06-21-2006 08:35 AM
RegEx search for a substring within a substring colinhumber@gmail.com Perl Misc 3 08-03-2005 04:29 PM
get the number of appearance of a substring in a String Java 13 04-14-2005 08:06 PM



Advertisments