Difference between revisions of "Remove non-letters from a string"

From CodeCodex

(JavaScript)
 
(25 intermediate revisions by 17 users not shown)
Line 1: Line 1:
[http://ruwier.125mb.com/kesenai-tsumi/ kesenai tsumi] [http://slavfondmeister.angelfire.com/styles/how-would.htm how would u feel] [http://wiellico.125mb.com/html/teen-sex.htm teen sex videos] [http://anethel.angelfire.com/comune-di.htm comune di treviso] [http://bedrew.125mb.com/bari-vibo.htm bari vibo valentia] [http://amarymuk.angelfire.com/html/telecomando-meliconi.htm telecomando meliconi] [http://amarymuk.angelfire.com/html/foto-offspring.htm foto offspring] [http://dleslis.125mb.com/resources/lettori-audio.htm lettori audio mp3] [http://vegrigsb.angelfire.com/resources/il-cuore.htm il cuore come amore] [http://dleslis.125mb.com/resources/duel-masters.htm duel masters vol 2] [http://tibrown.125mb.com/description/revolucion/ revolucion] [http://hoadmoni.125mb.com/styles/micro-maniacs/ micro maniacs] [http://gistrate.angelfire.com/html/inmortal.htm inmortal] [http://awihimmeru.angelfire.com/fotos-de.htm fotos de claveles] [http://nescogin.125mb.com/content/www-cinema.htm www cinema ugc fr] [http://nescogin.125mb.com/content/i-manga.htm i manga a 128 bit] [http://awihimmeru.angelfire.com/gadget-sportivi.htm gadget sportivi] [http://sewehner.125mb.com/articles/java-free/ java free] [http://kocarawf.125mb.com/blog/www-ciee.htm www ciee org br] [http://awihimmeru.angelfire.com/winzip-per.htm winzip per windows xp] [http://dleslis.125mb.com/resources/incredimail-gutterball.htm incredimail gutterball] [http://dleslis.125mb.com/resources/universita-dams.htm universita dams] [http://nescogin.125mb.com/content/jeonju.htm jeonju] [http://sewehner.125mb.com/articles/pentax-optio/ pentax optio mx] [http://mamans.125mb.com/topic/grand-theft.htm grand theft auto psp] [http://gistrate.angelfire.com/html/le-metropole.htm le metropole] [http://dacornel.125mb.com/data/chiarly.htm chiarly] [http://paryker.125mb.com/styles/midnight-club/ midnight club 3 la prossima settimana in italia] [http://stjollay.125mb.com/topic/micene/ micene] [http://awihimmeru.angelfire.com/epson-stylus.htm epson stylus photo 810] [http://yonosoyyo.angelfire.com/directory/pensiline.htm pensiline] [http://tibrown.125mb.com/description/celestina/ celestina] [http://hoadmoni.125mb.com/styles/cartelle-infermieristiche/ cartelle infermieristiche] [http://sawilson.125mb.com/data/cockatil.htm cockatil] [http://blhestrie.125mb.com/web/videoregistratore-super/ videoregistratore super vhs] [http://gitedy.125mb.com/blog/carpentier-y/ carpentier y valmont alejo] [http://wiellico.125mb.com/html/teac-gb.htm teac 40gb] [http://degaylor.125mb.com/torino-manila/ torino manila] [http://amarymuk.angelfire.com/html/timer-caffettiera.htm timer caffettiera] [http://wiellico.125mb.com/html/tm.htm tm 8100] [http://mamans.125mb.com/topic/mondo-erotico.htm mondo erotico] [http://yonosoyyo.angelfire.com/directory/www-bambola.htm www bambola club it] [http://morudd.angelfire.com/web/fabra-pompeu/ fabra pompeu] [http://gitedy.125mb.com/blog/pinocchia/ pinocchia] [http://paryker.125mb.com/styles/concorsi-carabinieri/ concorsi carabinieri riservato volontari] [http://sawilson.125mb.com/data/tachicardia.htm tachicardia] [http://nescogin.125mb.com/content/principessina.htm principessina] [http://stjollay.125mb.com/topic/braun-frullatore/ braun frullatore] [http://requidim.angelfire.com/web/ram-mhz.htm ram 133mhz 256mb] [http://fikimber.125mb.com/lib/doom-.htm doom 3 mod] [http://hoadmoni.125mb.com/styles/lavoro-a/ lavoro a legnago] [http://morudd.angelfire.com/web/siti-di/ siti di streghe] [http://sewehner.125mb.com/articles/biliardo-classics/ biliardo classics] [http://bedrew.125mb.com/casi-hong.htm casi hong nhung] [http://stjollay.125mb.com/topic/tn/ tn 11] [http://nescogin.125mb.com/content/www-virgilio.htm www virgilio it] [http://wiellico.125mb.com/html/linksys-ag.htm linksys ag241 modem] [http://dacornel.125mb.com/data/apple-imac.htm apple imac g5 17] [http://tibrown.125mb.com/description/takeshi-kitano/ takeshi kitano] [http://sawilson.125mb.com/data/adidas-scarpe.htm adidas scarpe skateboard] [http://morudd.angelfire.com/web/www-foxx/ www foxx equipment com] [http://dleslis.125mb.com/resources/cazzi-di.htm cazzi di cavallo] [http://paryker.125mb.com/styles/registratore-di/ registratore di minidisc] [http://wiellico.125mb.com/html/dragostea-din.htm dragostea din tei mid] [http://stjollay.125mb.com/topic/www-sat/ www sat com] [http://sibeatri.angelfire.com/html/kenwood-autoradio/ kenwood autoradio con dvd] [http://hoadmoni.125mb.com/styles/stampante-hp/ stampante hp designjet 110 plus] [http://sawilson.125mb.com/data/ratchet-gladiator.htm ratchet gladiator] [http://anethel.angelfire.com/ipod-cellulari.htm ipod cellulari] [http://dacornel.125mb.com/data/ddr-memoria.htm ddr2 memoria ram] [http://gistrate.angelfire.com/html/attrezzatura-campeggio.htm attrezzatura campeggio] [http://ruwier.125mb.com/hp-xeon/ hp xeon computer] [http://hoadmoni.125mb.com/styles/le-cap/ le cap] [http://stjollay.125mb.com/topic/i-gocha/ i gocha] [http://nescogin.125mb.com/content/lifeview-duo.htm lifeview duo] [http://gitedy.125mb.com/blog/scheda-asus/ scheda asus video] [http://tibrown.125mb.com/description/croak/ croak] [http://kocarawf.125mb.com/blog/una-data.htm una data per ace combat 4] [http://dacornel.125mb.com/data/hattaway.htm hattaway] [http://ruwier.125mb.com/change-your/ change your heart] [http://stjollay.125mb.com/topic/golf-iv/ golf iv volkswagen] [http://fikimber.125mb.com/lib/rock-roll.htm rock roll is dead] [http://hoadmoni.125mb.com/styles/robotics-modem/ robotics modem fax 56] [http://paryker.125mb.com/styles/web-mail/ web mail] [http://dacornel.125mb.com/data/tamburo-rullante.htm tamburo rullante] [http://amarymuk.angelfire.com/html/oasis-all.htm oasis all around the world] [http://anethel.angelfire.com/frontalino-sony.htm frontalino sony] [http://elernst.angelfire.com/articles/monika-brodka.htm monika brodka mp3] [http://hoadmoni.125mb.com/styles/elenco-partecipanti/ elenco partecipanti miss italia 2004] [http://ruwier.125mb.com/hp-business/ hp business inkjet 2800] [http://champ-alnite.angelfire.com/grazie-per.htm grazie per quel caldo dicembre] [http://sewehner.125mb.com/articles/pesca-tripode/ pesca tripode] [http://hoadmoni.125mb.com/styles/ospedale-de/ ospedale de best di milano] [http://blhestrie.125mb.com/web/amplificatori-classe/ amplificatori classe] [http://blhestrie.125mb.com/web/video-strani/ video strani] [http://yonosoyyo.angelfire.com/directory/le-motorizzate.htm le motorizzate] [http://sewehner.125mb.com/articles/que-es/ que es la administracion] [http://grdugans.angelfire.com/text/herrera.htm herrera] [http://vegrigsb.angelfire.com/resources/ragazze-di.htm ragazze di salerno] [http://sewehner.125mb.com/articles/canon/ canon 3200] [http://sewehner.125mb.com/articles/rs-dv/ rs dv multi media card] [http://dleslis.125mb.com/resources/manifestazioni-sanremo.htm manifestazioni sanremo] [http://degaylor.125mb.com/abbigliamento-uomo/ abbigliamento uomo pelle] [http://hoadmoni.125mb.com/styles/albergo-stella/ albergo stella] [http://ruwier.125mb.com/jeans-vita/ jeans vita bassa uomo] [http://sibeatri.angelfire.com/html/dsl-modem/ dsl modem firewall] [http://wiellico.125mb.com/html/hepu.htm hepu] [http://fikimber.125mb.com/lib/bert.htm bert] [http://sewehner.125mb.com/articles/veneto-moto/ veneto moto usate] [http://anethel.angelfire.com/quattro-nuovi.htm quattro nuovi dvd super] [http://yonosoyyo.angelfire.com/directory/auto-gas.htm auto gas] [http://degaylor.125mb.com/macchine-gare/ macchine gare] [http://blhestrie.125mb.com/web/stefania-belmondo/ stefania belmondo] [http://ruwier.125mb.com/siti-amatoriali/ siti amatoriali] [http://blhestrie.125mb.com/web/hp-all/ hp all in one a3] [http://hahosmer.angelfire.com/html/like-a.htm like a payer] [http://slavfondmeister.angelfire.com/styles/accessori-fujifilm.htm accessori fujifilm s5000] [http://slavfondmeister.angelfire.com/styles/nubira-gpl.htm nubira gpl] [http://degaylor.125mb.com/danger-zone/ danger zone 2] [http://tibrown.125mb.com/description/dagostea-tea/ dagostea tea din tei] [http://morudd.angelfire.com/web/athlon-/ athlon 64 3200 core venice] [http://blhestrie.125mb.com/web/inchiostri-solidi/ inchiostri solidi] [http://tibrown.125mb.com/description/roma-delhi/ roma delhi biglietti aerei] [http://morudd.angelfire.com/web/volo-last/ volo last minute canarie] [http://bedrew.125mb.com/trucci.htm trucci] [http://hoadmoni.125mb.com/styles/bad-moon/ bad moon rising] [http://amarymuk.angelfire.com/html/rio-diablos.htm rio diablos] [http://tibrown.125mb.com/description/jane-carrera/ jane carrera] [http://fikimber.125mb.com/lib/tha-calling.htm tha calling] [http://spdinnie.angelfire.com/html/cinquantenni/ cinquantenni] [http://yonosoyyo.angelfire.com/directory/seredova-triumph.htm seredova triumph] [http://yonosoyyo.angelfire.com/directory/bc.htm bc 10] [http://stjollay.125mb.com/topic/il-figlio/ il figlio del falegname] [http://kocarawf.125mb.com/blog/raffy-matias.htm raffy matias] [http://anethel.angelfire.com/radeon-.htm radeon 9250 128mb] [http://gitedy.125mb.com/blog/fotostorie/ fotostorie] [http://requidim.angelfire.com/web/canti-popolari.htm canti popolari piemontesi] [http://ruwier.125mb.com/rs-n/ rs80 n3] [http://ruwier.125mb.com/ciccio-ingrassia/ ciccio ingrassia] [http://gistrate.angelfire.com/html/ti-sposero.htm ti sposero perche eros ramazzotti] [http://blhestrie.125mb.com/web/benessere-sexy/ benessere sexy] [http://kocarawf.125mb.com/blog/musica-contro.htm musica contro il potere e le ingiustizie] [http://kocarawf.125mb.com/blog/marketing-rc.htm marketing rc] [http://fikimber.125mb.com/lib/video-k.htm video 56k] [http://grdugans.angelfire.com/text/cavalieri-dello.htm cavalieri dello zodiaco dvd] [http://stjollay.125mb.com/topic/the-mohicans/ the mohicans] [http://gistrate.angelfire.com/html/lcd-thomson.htm lcd thomson] [http://sewehner.125mb.com/articles/acer-/ acer  cr6530] [http://hahosmer.angelfire.com/html/vob-edit.htm vob edit] [http://hoadmoni.125mb.com/styles/ad-libitum/ ad libitum] [http://sewehner.125mb.com/articles/in-culo/ in culo] [http://anethel.angelfire.com/www-tj.htm www tj net] [http://ruwier.125mb.com/drama-queen/ drama queen] [http://champ-alnite.angelfire.com/iasi-provincia.htm iasi (provincia)] [http://anethel.angelfire.com/hp-hw.htm hp hw 6515] [http://stjollay.125mb.com/topic/oling-nor/ oling nor] [http://vegrigsb.angelfire.com/resources/da-apple.htm da apple store] [http://dacornel.125mb.com/data/clinica-s.htm clinica s raffaele] [http://morudd.angelfire.com/web/marcie-nuziali/ marcie nuziali] [http://stjollay.125mb.com/topic/cara-mia/ cara mia ex] [http://mamans.125mb.com/topic/comune-di.htm comune di piombino] [http://degaylor.125mb.com/gli-ultimi/ gli ultimi sei minuti] [http://gitedy.125mb.com/blog/finanziamento-peschiera/ finanziamento peschiera garda] [http://amarymuk.angelfire.com/html/obiettivi-nikkor.htm obiettivi nikkor 1870] [http://amarymuk.angelfire.com/html/wings-srl.htm wings srl import export] [http://dacornel.125mb.com/data/filastrocche-sarde.htm filastrocche sarde] [http://tibrown.125mb.com/description/selvicoltura/ selvicoltura] [http://sewehner.125mb.com/articles/calendari-maschi/ calendari maschi] [http://sewehner.125mb.com/articles/uomo-di/ uomo di cera] [http://sewehner.125mb.com/articles/formula/ formula1 92] [http://tibrown.125mb.com/description/eritema-nodoso/ eritema nodoso] [http://nescogin.125mb.com/content/nuova-mercedes.htm nuova mercedes sprinter diesel auto] [http://tibrown.125mb.com/description/www-rebate/ www rebate online com] [http://spdinnie.angelfire.com/html/classic-gaggia/ classic gaggia] [http://dleslis.125mb.com/resources/piscina-sicilia.htm piscina sicilia] [http://nescogin.125mb.com/content/gioci-giochi.htm gioci giochi giochi] [http://spdinnie.angelfire.com/html/canggung/ canggung] [http://fikimber.125mb.com/lib/ragazze-di.htm ragazze di legnano] [http://gistrate.angelfire.com/html/chirurgo.htm chirurgo] [http://gitedy.125mb.com/blog/intel-/ intel 560 3 6ghz] [http://wiellico.125mb.com/html/you-have.htm you have been loved] [http://nescogin.125mb.com/content/lg.htm lg 9900] [http://vegrigsb.angelfire.com/resources/pallavolo-a.htm pallavolo a2 femminile] [http://paryker.125mb.com/styles/hotel-el/ hotel el olf hamammet] [http://mamans.125mb.com/topic/modular-system.htm modular system] [http://wiellico.125mb.com/html/primolevi.htm primolevi] [http://ruwier.125mb.com/assistenza-thomson/ assistenza thomson firenze] [http://amarymuk.angelfire.com/html/download-per.htm download per mixare mp3] [http://anethel.angelfire.com/muhammet.htm muhammet] [http://degaylor.125mb.com/topline-com/ topline com] [http://sawilson.125mb.com/data/video-eros.htm video eros ramazotti] [http://mamans.125mb.com/topic/conflitto-iraq.htm conflitto iraq] [http://nescogin.125mb.com/content/hard-disk.htm hard disk foto] [http://wiellico.125mb.com/html/bluethoot-wireless.htm bluethoot wireless] [http://gitedy.125mb.com/blog/ufficio-isola/ ufficio isola giglio] [http://kocarawf.125mb.com/blog/rete-pingpong.htm rete pingpong] [http://fikimber.125mb.com/lib/federica-rispoli.htm federica rispoli] [http://nescogin.125mb.com/content/in-italia.htm in italia si fa sesso orale] [http://bedrew.125mb.com/navigatori-garmin.htm navigatori garmin 320] [http://requidim.angelfire.com/web/seagate-gb.htm seagate 100gb usb] {{Infobox See Also String}}
+
{{Infobox See Also String}}
 
==Implementations==
 
==Implementations==
 +
===C===
 +
if buff's content is: "15+41-2Hel54lo **1212 Wor2ld! Ho5w Are 6996 Yo7u?" then
 +
the content of buff_02 will be: "HelloWorldHowAreYou"
 +
<pre class="c">#include <string.h>
 +
 +
while (i != strlen (buff))
 +
{
 +
if ((buff[i] >= 65) && (buff[i] <= 90) || (buff[i] >= 97) && (buff[i] <= 122))
 +
{
 +
buff_02[j] = buff[i];
 +
i++;
 +
j++;
 +
}
 +
else
 +
{
 +
i++;
 +
}
 +
}
 +
</pre>
 +
 +
===Erlang===
 +
<pre>
 +
S = "how19 a*re 254y**ou?".
 +
re:replace(S, "[^A-Za-z]", "", [global, {return, list}]).
 +
</pre>
 +
 +
===Haskell===
 +
<pre>
 +
import Data.Char ( isLetter )
 +
 +
removeNonLetters = filter isLetter
 +
</pre>
 +
 +
In ghci:
 +
<pre>
 +
*Main> :t removeNonLetters
 +
removeNonLetters :: [Char] -> [Char]
 +
*Main> removeNonLetters "15+41-2Hel54lo **1212 Wor2ld! Ho5w Are 6996 Yo7u?"
 +
"HelloWorldHowAreYou"
 +
</pre>
 +
 
===Java===
 
===Java===
 
This code removes common symbols and numbers from a string, returning letters only.  (Note, not all symbols are included.)
 
This code removes common symbols and numbers from a string, returning letters only.  (Note, not all symbols are included.)
 +
<pre class="java">
 
  static String lettersOnly(String s) {
 
  static String lettersOnly(String s) {
     return s.replaceAll("[0-9]|[ !@#\\$%\\^
+
     return s.replaceAll("[^a-zA-Z]","");
 +
}
 +
</pre>
 +
 
 +
===JavaScript===
 +
<pre class="javascript">
 +
var s = "how19 a*re 254y**ou?";
 +
 
 +
document.write(s.replace(/[^A-Za-z]/g, ""));
 +
</pre>
 +
 
 +
This didn't work for me.  parseInt(string) would.
 +
 
 +
===Objective-C===
 +
<pre>
 +
@interface NSString (RemoveNonLetters)
 +
- (NSString *)removeNonLetters;
 +
@end
 +
 
 +
@implementation NSString (RemoveNonLetters)
 +
- (NSString *)removeNonLetters {
 +
  return [[self componentsSeparatedByCharactersInSet:[[NSCharacterSet letterCharacterSet] invertedSet]] componentsJoinedByString:@""];
 +
}
 +
@end
 +
</pre>
 +
For example:
 +
<pre>
 +
NSString *s = @"133t H4x0r";
 +
NSLog(@"%@", [s removeNonLetters]); // prints "tHxr"
 +
</pre>
 +
 
 +
===OCaml===
 +
<pre>
 +
# let remove_nonalpha = Str.global_replace (Str.regexp "[^a-zA-Z]+") "";;
 +
val remove_nonalpha : string -> string = <fun>
 +
</pre>
 +
For example:
 +
<pre>
 +
# remove_nonalpha "133t H4x0r";;
 +
- : string = "tHxr"
 +
</pre>
 +
 
 +
===Perl===
 +
<pre>
 +
s/[\W\d_]//g; # remove all non-word characters and digits and underscores
 +
</pre>
 +
 
 +
===PHP===
 +
<pre class="php">
 +
$result = preg_replace('/[^A-Za-z]/', '', $string);
 +
</pre>
 +
 
 +
===Python===
 +
<pre class="python">
 +
s = "hello world! how are you? 0"
 +
 
 +
# Short version
 +
print filter(lambda c: c.isalpha(), s)
 +
 
 +
# Faster version for long ASCII strings:
 +
id_tab = "".join(map(chr, xrange(256)))
 +
tostrip = "".join(c for c in id_tab if c.isalpha())
 +
print s.translate(id_tab, tostrip)
 +
 
 +
# Using regular expressions
 +
print re.sub("[^A-Za-z]", "", s)
 +
</pre>
 +
 
 +
===Ruby===
 +
<pre class="ruby">
 +
s = "how19 a*re 254y**ou?";
 +
puts s.delete("^a-zA-Z")
 +
puts s.gsub(/[^a-zA-Z]/, "")
 +
puts s.gsub(/(\W|\d)/, "")
 +
</pre>
 +
 
 +
=== Tcl ===
 +
<pre>
 +
proc remove_non_ascii_letters s {regsub -all {[^a-zA-Z]} $s ""}
 +
</pre>
 +
<pre>
 +
remove_non_ascii_letters "hello world! how are you? 0é" ;# -> helloworldhowareyou
 +
</pre>
 +
===Visual Basic===
 +
<pre class="vb">
 +
Public Function ReturnAlpha(ByVar sString As String) As String
 +
Dim i As Integer
 +
For i = 1 To Len(sString)
 +
    If Mid(sString, i, 1) Like "[A-Za-z]" Then
 +
        ReturnAlpha = ReturnAlpha + Mid(sString, i, 1)
 +
    End If
 +
Next i
 +
End Function
 +
</pre>
 +
 
 +
===WinBatch===
 +
<pre>
 +
; Remove non-letters from a string, assuming space as non-letter character.
 +
 
 +
strString = "15+41-2Hel54lo **1212 Wor2ld! Ho5w Are 6996 Yo7u?"
 +
 
 +
; Example 1
 +
strClean1 = StrClean (strString, "0123456789+-*/!?.,;:~@ ", "", @TRUE, 1)
 +
; strClean1 = "HelloWorldHowAreYou"
 +
 
 +
; Example 2
 +
strClean2 = StrClean (strString, "abcdefghijklmnopqrstuvwxyz", "", @FALSE, 2)
 +
; strClean2 = "HelloWorldHowAreYou"
 +
 
 +
; Example 3
 +
objRegExp = ObjectCreate ("VBScript.RegExp")
 +
objRegExp.IgnoreCase = @TRUE
 +
objRegExp.MultiLine = @FALSE
 +
objRegExp.Global = @TRUE
 +
objRegExp.Pattern = "[^a-z]"
 +
strClean3 = objRegExp.Replace(strString, "")
 +
objRegExp = 0
 +
; strClean3 = "HelloWorldHowAreYou"
 +
 
 +
Exit
 +
; This code example was written by Detlev Dalitz.
 +
</pre>
 +
 
 +
===Zsh===
 +
<pre>
 +
s="Hello, World! How are you? 1... 2... 3..."
 +
print "${s//[^a-zA-Z]/}"
 +
</pre>
 +
 
 +
[[Category:String]]
 +
[[Category:C]]
 +
[[Category:Erlang]]
 +
[[Category:Java]]
 +
[[Category:JavaScript]]
 +
[[Category:Objective Caml]]
 +
[[Category:Perl]]
 +
[[Category:PHP]]
 +
[[Category:Python]]
 +
[[Category:Ruby]]
 +
[[Category:Tcl]]
 +
[[Category:Visual Basic]]
 +
[[Category:WinBatch]]
 +
[[Category:Zsh]]

Latest revision as of 19:47, 3 May 2012

Related content:

Implementations[edit]

C[edit]

if buff's content is: "15+41-2Hel54lo **1212 Wor2ld! Ho5w Are 6996 Yo7u?" then the content of buff_02 will be: "HelloWorldHowAreYou"

#include <string.h>

while (i != strlen (buff))
{
	if ((buff[i] >= 65) && (buff[i] <= 90) || (buff[i] >= 97) && (buff[i] <= 122))
	{
		buff_02[j] = buff[i];
		i++;
		j++;
	}
	else
	{
		i++;
	}
}

Erlang[edit]

S = "how19 a*re 254y**ou?".
re:replace(S, "[^A-Za-z]", "", [global, {return, list}]).

Haskell[edit]

import Data.Char ( isLetter )

removeNonLetters = filter isLetter

In ghci:

*Main> :t removeNonLetters 
removeNonLetters :: [Char] -> [Char]
*Main> removeNonLetters "15+41-2Hel54lo **1212 Wor2ld! Ho5w Are 6996 Yo7u?"
"HelloWorldHowAreYou"

Java[edit]

This code removes common symbols and numbers from a string, returning letters only. (Note, not all symbols are included.)

 static String lettersOnly(String s) {
    return s.replaceAll("[^a-zA-Z]","");
 }

JavaScript[edit]

var s = "how19 a*re 254y**ou?";

document.write(s.replace(/[^A-Za-z]/g, ""));

This didn't work for me. parseInt(string) would.

Objective-C[edit]

@interface NSString (RemoveNonLetters)
- (NSString *)removeNonLetters;
@end

@implementation NSString (RemoveNonLetters)
- (NSString *)removeNonLetters {
  return [[self componentsSeparatedByCharactersInSet:[[NSCharacterSet letterCharacterSet] invertedSet]] componentsJoinedByString:@""];
}
@end

For example:

NSString *s = @"133t H4x0r";
NSLog(@"%@", [s removeNonLetters]); // prints "tHxr"

OCaml[edit]

 # let remove_nonalpha = Str.global_replace (Str.regexp "[^a-zA-Z]+") "";;
 val remove_nonalpha : string -> string = <fun>

For example:

 # remove_nonalpha "133t H4x0r";;
 - : string = "tHxr"

Perl[edit]

 s/[\W\d_]//g; # remove all non-word characters and digits and underscores

PHP[edit]

$result = preg_replace('/[^A-Za-z]/', '', $string);

Python[edit]

s = "hello world! how are you? 0"

# Short version
print filter(lambda c: c.isalpha(), s)

# Faster version for long ASCII strings:
id_tab = "".join(map(chr, xrange(256)))
tostrip = "".join(c for c in id_tab if c.isalpha())
print s.translate(id_tab, tostrip)

# Using regular expressions
print re.sub("[^A-Za-z]", "", s)

Ruby[edit]

s = "how19 a*re 254y**ou?";
puts s.delete("^a-zA-Z")
puts s.gsub(/[^a-zA-Z]/, "")
puts s.gsub(/(\W|\d)/, "")

Tcl[edit]

 proc remove_non_ascii_letters s {regsub -all {[^a-zA-Z]} $s ""}
 remove_non_ascii_letters "hello world! how are you? 0é" ;# -> helloworldhowareyou

Visual Basic[edit]

Public Function ReturnAlpha(ByVar sString As String) As String
Dim i As Integer
For i = 1 To Len(sString)
    If Mid(sString, i, 1) Like "[A-Za-z]" Then
        ReturnAlpha = ReturnAlpha + Mid(sString, i, 1)
    End If
Next i
End Function

WinBatch[edit]

; Remove non-letters from a string, assuming space as non-letter character.

strString = "15+41-2Hel54lo **1212 Wor2ld! Ho5w Are 6996 Yo7u?"

; Example 1
strClean1 = StrClean (strString, "0123456789+-*/!?.,;:~@ ", "", @TRUE, 1) 
; strClean1 = "HelloWorldHowAreYou"

; Example 2
strClean2 = StrClean (strString, "abcdefghijklmnopqrstuvwxyz", "", @FALSE, 2) 
; strClean2 = "HelloWorldHowAreYou"

; Example 3
objRegExp = ObjectCreate ("VBScript.RegExp")
objRegExp.IgnoreCase = @TRUE
objRegExp.MultiLine = @FALSE
objRegExp.Global = @TRUE
objRegExp.Pattern = "[^a-z]"
strClean3 = objRegExp.Replace(strString, "") 
objRegExp = 0
; strClean3 = "HelloWorldHowAreYou"

Exit
; This code example was written by Detlev Dalitz.

Zsh[edit]

s="Hello, World! How are you? 1... 2... 3..."
print "${s//[^a-zA-Z]/}"