MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Finds strings in a list that begin with "A"

  • To: mathgroup at smc.vnet.net
  • Subject: [mg82333] Re: Finds strings in a list that begin with "A"
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Thu, 18 Oct 2007 04:43:52 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <ff4egn$d6p$1@smc.vnet.net>

tomfabtastic at hotmail.com wrote:

> I am trying to look through a list of strings and return all strings
> that start with an "A".
> 
> For example, my list could be :
> 
> {"And", "Bat", "Artistic", "Cob"} so I want to return {"And",
> "Artistic"}.
> 
> Any ideas ?

One possible way of doing this is as follows. *StringCases* is applied 
in turn to each element of the list thanks to *Map* (shortcut /@) and 
check whether the current string start with a capital A followed by at 
least one more character (use triple dashes ___ if you want zero, one, 
or more characters). Also, you should look at the online documentation 
about *StringExpression* to get an overall idea of the possibilities.

In[1]:= lst = {"And", "Bat", "Artistic", "Cob"};

In[2]:= StringCases[#, "A" ~~ __] & /@ lst // Flatten

Out[2]= {"And", "Artistic"}

Regards,
-- 
Jean-Marc


  • Prev by Date: Re: Help with NMinimize
  • Next by Date: RE: Finds strings in a list that begin with "A"
  • Previous by thread: Re: Finds strings in a list that begin with "A"
  • Next by thread: Re: Finds strings in a list that begin with "A"