Erlang Central

Difference between revisions of "String Check For Char"

From ErlangCentral Wiki

m
Line 24: Line 24:
  
 
[[Category:CookBook]][[Category:StringRecipes]]
 
[[Category:CookBook]][[Category:StringRecipes]]
 
 
 
[http://www.web-craps.com craps]
 
[http://www.magical-casino.com/casino_download.html Casino Downloading Action.]
 
[http://www.online-casino-wiki.com/index.php/online_casino online casino]
 
[http://www.online-casino-wiki.com/index.php/online_casino online casino]
 
[http://www.bestweb-online-casinos.com/slots-game/diamond-club-slots.html diamond club slots]
 
[http://www.casino-theory.com/online-casino-royale/online-casino-gamble.html online casino gamble]
 
[http://www.casinos-new.com/blackjack.html Online casino - Blackjack online]
 
[http://www.casino-theory.com/online-casino-royale/online-casino-gamble.html online casino gamble]
 
[http://www.casino-theory.com/online-casino-bonus/index.html online casino bonus]
 
[http://www.casino-theory.com/online-casino-bonus/online-casino-net.html online casino net]
 

Revision as of 14:57, 21 November 2006

Problem

You need to find out if a string contains a character. You probably want to know the index of where the character was found.

Solution

Use the string library string:chr and string:rchr functions. string:chr searches a string for a character (starting from the left hand side of the string), and returns its index. string;rchr performs the same task, starting from the right hand side. Note that all Erlang indexes are 1-based:

1> string:chr("Hello, World!", $,).
6
2> string:chr("My, this is a lot of commas, I guess.", $,).
3
3> string:rchr("My, this is a lot of commas, I guess.", $,).
28

string:chr and string:rchr return 0 if no match is found:

4> string:chr("This sentence has no commas.", $,).
0
5> string:rchr("This sentence has no commas.", $,).
0