Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Initial letters of the words in a string

Reply
Thread Tools

Initial letters of the words in a string

 
 
Swifty
Guest
Posts: n/a
 
      09-08-2011
Given a string of words such as "The quick brown fox jumps over the
lazy dog", how easy would it be to generate a string containing the
initial letters of the words, in lower case, e.g. 'tqbfjotld'

As it happens, the string will come from the content (value) of one
<INPUT TYPE=TEXT> control, and the output will replace the content
(value) of another INPUT control on a user action, such as a mouse
click.

I don't have a page to demonstrate how far I've already got, as the
idea only just occurred to me. I can manage the HTML, and the
JavaScript to get the value of the first INPUT control, and then set
the content of the second INPUT control.

What I'm looking for is the JavaScript equivalent of my initials()
function that I have in my REXX subroutines library:

Inits = ''
Do I = 1 to words(string)
Inits = Inits || left(word(string,I),1)
End

--
Steve Swift
http://www.swiftys.org.uk/swifty.html
http://www.ringers.org.uk
 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      09-08-2011
Swifty wrote on 08 sep 2011 in comp.lang.javascript:

> Given a string of words such as "The quick brown fox jumps over the
> lazy dog", how easy would it be to generate a string containing the
> initial letters of the words, in lower case, e.g. 'tqbfjotld'
>
> As it happens, the string will come from the content (value) of one
> <INPUT TYPE=TEXT> control, and the output will replace the content
> (value) of another INPUT control on a user action, such as a mouse
> click.
>
> I don't have a page to demonstrate how far I've already got, as the
> idea only just occurred to me. I can manage the HTML, and the
> JavaScript to get the value of the first INPUT control, and then set
> the content of the second INPUT control.
>
> What I'm looking for is the JavaScript equivalent of my initials()
> function that I have in my REXX subroutines library:
>
> Inits = ''
> Do I = 1 to words(string)
> Inits = Inits || left(word(string,I),1)
> End


> ... how easy would it be ....


It is easy in Javascript.
We should, however, not do your schoolwork.

Remember that Javascript has Regular Expressions,
so amaze your teacher with a fast and compact solution,
using just on line without loops.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
 
 
 
Jukka K. Korpela
Guest
Posts: n/a
 
      09-08-2011
8.9.2011 18:27, Swifty wrote:

> Given a string of words such as "The quick brown fox jumps over the
> lazy dog", how easy would it be to generate a string containing the
> initial letters of the words, in lower case, e.g. 'tqbfjotld'


It depends on the exact definition of "word" and "letter", but assuming
very simple definitions (a word is a maximal sequence of non-space
letters, and any non-space character qualifies as "first letter"), quite
simple:

<!doctype html>
<title>Extract first letters</title>
<input id=data size=50 style="width: 100%">
<input type=button value=Extract onclick=
"document.getElementById('result').value =
firstLetters(document.getElementById('data').value ) ">
<p>
<input id=result>
<script>
function firstLetters(str) {
var words = str.split(' ');
var result = '';
var i;
for(i = 0; i < words.length; i++) {
result += words[i].charAt(0).toLowerCase();
}
return result;
}
</script>

I have assumed that the first character needs to be converted to
lowercase. This part too is easy _unless_ you need to consider the case
where the text is in Turkish, Azeri, or another language that
distinguishes between normal i and dotless i.

If "letter" means literally "letter" as an alphabetic character, then
things get rather difficult. There is no direct way in JavaScript to
decide whether a given character is alphabetic. It would still be
doable, but laborious, and you would need to fix the concept of "letter"
_somehow_ (e.g., "a character defined as a letter in Unicode version 6.0").

Then again, if "letter" means just any simple Latin letter from "a" to
"z", then things are fairly simple.

> What I'm looking for is the JavaScript equivalent of my initials()
> function that I have in my REXX subroutines library:
>
> Inits = ''
> Do I = 1 to words(string)
> Inits = Inits || left(word(string,I),1)
> End


It was _ages_ ago when I used REXX, and not much, but I guess that the
code works with rather simple concepts of "word" and "letter". But
"word" might differ from the simplest definition - for example, would
the "words" in "Hello, world!" be "Hello," and "world!" or "Hello" and
"world"?


--
Yucca, http://www.cs.tut.fi/~jkorpela/
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      09-08-2011
Swifty wrote:

> Given a string of words such as "The quick brown fox jumps over the
> lazy dog", how easy would it be to generate a string containing the
> initial letters of the words, in lower case, e.g. 'tqbfjotld'


Very easy. Assuming that "word" refers to the ECMAScript meaning:

"The quick brown fox jumps over the lazy dog"
.match(/\b\S/g).join("").toLowerCase()


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      09-08-2011
Thomas 'PointedEars' Lahn wrote on 08 sep 2011 in comp.lang.javascript:

> Swifty wrote:
>
>> Given a string of words such as "The quick brown fox jumps over the
>> lazy dog", how easy would it be to generate a string containing the
>> initial letters of the words, in lower case, e.g. 'tqbfjotld'

>
> Very easy. Assuming that "word" refers to the ECMAScript meaning:
>
> "The quick brown fox jumps over the lazy dog"
> .match(/\b\S/g).join("").toLowerCase()


s = s.replace(/\s*(\S)\S*(\s+|$)/g,'$1').toLowerCase()


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      09-09-2011
On Sep 9, 3:47*am, "Evertjan." <(E-Mail Removed)> wrote:
> Thomas 'PointedEars' Lahn wrote on 08 sep 2011 in comp.lang.javascript:
>
> > Swifty wrote:

>
> >> Given a string of words such as "The quick brown fox jumps over the
> >> lazy dog", how easy would it be to generate a string containing the
> >> initial letters of the words, in lower case, e.g. 'tqbfjotld'

>
> > Very easy. *Assuming that "word" refers to the ECMAScript meaning:

>
> > * "The quick brown fox jumps over the lazy dog"
> > * .match(/\b\S/g).join("").toLowerCase()

>
> s = s.replace(/\s*(\S)\S*(\s+|$)/g,'$1').toLowerCase()


why not:

s.replace(/\b(\w)\w*\s*/g,'$1').toLowerCase()

Homework complete - until the asked "how does it work?"

--
Rob
 
Reply With Quote
 
Swifty
Guest
Posts: n/a
 
      09-09-2011
On Thu, 08 Sep 2011 19:18:22 +0300, "Jukka K. Korpela"
<(E-Mail Removed)> wrote:

>It depends on...


Yes, blank delimited words, probably nothing but a-z and maybe 0-9.

So far, the webpage concerned is used only in the UK and South Africa,
but could spread anywhere where my corporate employer does business.
I have safety net, however; I'm retiring in under two years, so my
"stuff" is unlikely to spread much further while I'm still supporting
it.
If they run into problems after I'm gone, I'll recommend that they ask
you; the hourly rates for consultancy are attractive.

Your example does exactly what I was hoping for, so, sometimes you
don't have to be careful what you wish for.

--
Steve Swift
http://www.swiftys.org.uk/swifty.html
http://www.ringers.org.uk
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      09-09-2011
RobG wrote on 09 sep 2011 in comp.lang.javascript:

> On Sep 9, 3:47*am, "Evertjan." <(E-Mail Removed)> wrote:
>> Thomas 'PointedEars' Lahn wrote on 08 sep 2011 in comp.lang.javascript:
>>
>> > Swifty wrote:

>>
>> >> Given a string of words such as "The quick brown fox jumps over the
>> >> lazy dog", how easy would it be to generate a string containing the
>> >> initial letters of the words, in lower case, e.g. 'tqbfjotld'

>>
>> > Very easy. *Assuming that "word" refers to the ECMAScript meaning:

>>
>> > * "The quick brown fox jumps over the lazy dog"
>> > * .match(/\b\S/g).join("").toLowerCase()

>>
>> s = s.replace(/\s*(\S)\S*(\s+|$)/g,'$1').toLowerCase()

>
> why not:
>
> s.replace(/\b(\w)\w*\s*/g,'$1').toLowerCase()


Not, because starting spaces are not filtered out.

> Homework complete - until the asked "how does it work?"


Indeed, the op will not have learned much.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
Swifty
Guest
Posts: n/a
 
      09-09-2011
On Thu, 8 Sep 2011 19:57:38 -0700 (PDT), RobG <(E-Mail Removed)>
wrote:

>Homework complete - until the asked "how does it work?"


As a matter of principle, I don't put into production any code that I
couldn't explain to a bright child.

As for all the "homework" comments: of course it's homework; I work
from home. Here's the view from my "office" over the top of my
display: http://swiftys.org.uk/images/home_office.jpg

It sure is nice to have you people helping me with my homework. Just
don't expect to help me with spending my salary. I've got a wife for
that

--
Steve Swift
http://www.swiftys.org.uk/swifty.html
http://www.ringers.org.uk
 
Reply With Quote
 
Scott Sauyet
Guest
Posts: n/a
 
      09-09-2011
Swifty wrote:
> It sure is nice to have you people helping me with my homework. Just
> don't expect to help me with spending my salary. I've got a wife for
> that


Thanks for your good humor about this. It does *sound* like a
homework problem. But your posting equivalent code in Rexx probably
should have lent you some legitimacy (unless CS programs are now
teaching Rexx as a prerequisite to JS )

I am amused by those who posted solutions but still thought it to be
homework, though. Why not post hints and suggestions until the OP
actually coughs up some code?

-- Scott
 
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
command prompt history filtered by initial letters limit Python 4 05-01-2009 12:26 AM
command prompt history filtered by initial letters limit Python 1 04-29-2009 10:38 PM
Replace stop words (remove words from a string) BerlinBrown Python 6 01-17-2008 02:37 PM
making all letters Caps/Small Letters Merrigan Python 4 12-14-2007 10:10 AM
split camelcase string into array of words words pantagruel Javascript 8 07-22-2006 07:46 PM



Advertisments