Quantcast

Matching a character in a string

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Matching a character in a string

Fabrice Delente-3-3
Hello.

Sorry for this very basic question but I didn't find (of didn't understand)
the answer either in the help or in the online guide.

I have a string, let's say "hello". I want to know if there is some
character in it, let's say an 'o'.

I tried

> charmatch("o", strstplit("hello",""))

but it gives NA (why??)

Thinking it may be a type problem, I tried

> charmatch("o",as.character(strsplit("hello","")))

but it gives NA too (why???)

So how can I know if a given string contains a given char?

Thanks!

--
Fabrice DELENTE

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [R] Matching a character in a string

Paul Hiemstra
Fabrice DELENTE wrote:

> Hello.
>
> Sorry for this very basic question but I didn't find (of didn't understand)
> the answer either in the help or in the online guide.
>
> I have a string, let's say "hello". I want to know if there is some
> character in it, let's say an 'o'.
>
> I tried
>
>  
>> charmatch("o", strstplit("hello",""))
>>    
>
> but it gives NA (why??)
>
> Thinking it may be a type problem, I tried
>
>  
>> charmatch("o",as.character(strsplit("hello","")))
>>    
>
> but it gives NA too (why???)
>
> So how can I know if a given string contains a given char?
>
> Thanks!
>
>  
try:

grep("o", "hello")

if the output is 1, there is a match. Note that the command is
vectorized so:

grep("o", c("hello","pizza", "spam", "ogle"))

also works, picking out items 1 and 4 as containing an "o". The first
argument to grep is a regular expression, and this can be very powerful.
For example, which words start with a "p":

grep("^p", c("hello","pizza", "spam", "ogle"))

Check out more stuff on regular expressions on the internet. They are
very powerful, but take some time to learn.

cheers,
Paul

--
Drs. Paul Hiemstra
Department of Physical Geography
Faculty of Geosciences
University of Utrecht
Heidelberglaan 2
P.O. Box 80.115
3508 TC Utrecht
Phone:  +3130 274 3113 Mon-Tue
Phone:  +3130 253 5773 Wed-Fri
http://intamap.geo.uu.nl/~paul

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Matching a character in a string

GlenB
In reply to this post by Fabrice Delente-3-3
The output from strsplit("hello","") is not suitable as input for the second argument for charmatch.

compare with:

 charmatch("o",strsplit("hello","")[[1]])
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Matching a character in a string

GlenB
See also ?unlist  (you can unlist(strsplit(...)) for the same effect
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [R] Matching a character in a string

Fabrice Delente-3-3
In reply to this post by Paul Hiemstra
> grep("o", "hello")

Thanks, I hadn't noticed R has grep included!

--
Fabrice DELENTE

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Loading...