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

From CodeCodex

(JavaScript)
 
(33 intermediate revisions by 22 users not shown)
Line 1: Line 1:
[http://tizolaa.ifrance.com/web/colores/ colores] [http://highbulp.ifrance.com/images/small/interflora-chieti/ interflora chieti] [http://oiyaoi.ifrance.com/images/small/croma-latina/ croma latina lyrics] [http://lovereceier.ifrance.com/lib/microsoft-offi.htm microsoft offi] [http://broadwaylili.ifrance.com/content/view/pile-aa.htm pile aa ricaricabili] [http://tizolaa.ifrance.com/web/dio-dei/ dio dei boschi] [http://singaporepets.ifrance.com/library/html/ricetrasmittenti.htm ricetrasmittenti] [http://jdawsona.ifrance.com/data/router-us/ router us robotics wireless] [http://oiyaoi.ifrance.com/images/small/pentium-/ pentium 4 3 8 ghz socket 775] [http://broadwaylili.ifrance.com/content/view/pianoteacher.htm pianoteacher] [http://broadwaylili.ifrance.com/content/view/divx-dvp.htm divx dvp630] [http://toofarfrommaybe.ifrance.com/view/new/yves-saint/ yves saint laurent cinema] [http://girlwho-is.ifrance.com/img/styles/new-beetle.htm new beetle como] [http://noxuhax.ifrance.com/content/view/lui.htm lui] [http://oiyaoi.ifrance.com/images/small/pierobon/ pierobon] [http://kalian42.ifrance.com/text/directory/aramo-pasquale/ aramo pasquale] [http://deviantrus.ifrance.com/topic/lettino-da/ lettino da massaggi] [http://deviantrus.ifrance.com/topic/chicco-rodeo/ chicco rodeo] [http://jdawsona.ifrance.com/data/samsung/ samsung 335] [http://igaros.ifrance.com/lib/germe-di/ germe di grano] [http://tizolaa.ifrance.com/web/culi-da/ culi da sballo] [http://highbulp.ifrance.com/images/small/strumien/ strumien] [http://igaros.ifrance.com/lib/albergo-lido/ albergo lido jesolo] [http://lovereceier.ifrance.com/lib/linfoma.htm linfoma] [http://toofarfrommaybe.ifrance.com/view/new/athlon/ athlon64 3700] [http://demurediablo.ifrance.com/library/html/monna-lisa/ monna lisa] [http://toofarfrommaybe.ifrance.com/view/new/repubblica-ceca/ repubblica ceca hotel] [http://deviantrus.ifrance.com/topic/biotera/ biotera] [http://kalian42.ifrance.com/text/directory/palmari-gps/ palmari gps phone] [http://noxuhax.ifrance.com/content/view/le-avventure.htm le avventure di frank] [http://broadwaylili.ifrance.com/content/view/lunar-silver.htm lunar silver star] [http://kalian42.ifrance.com/text/directory/kit-car/ kit car] [http://tat-ooin.ifrance.com/resources/articles/guetar.htm guetar] [http://deviantrus.ifrance.com/topic/ammortizzatori-sociali/ ammortizzatori sociali] [http://demurediablo.ifrance.com/library/html/bestemmi/ bestemmi] [http://tat-ooin.ifrance.com/resources/articles/canzoni-italiane.htm canzoni italiane] [http://deviantrus.ifrance.com/topic/dv-in/ dv in] [http://broadwaylili.ifrance.com/content/view/matiz-star.htm matiz star] [http://speedofsoun.ifrance.com/text/directory/fiabilandia.htm fiabilandia] [http://lovereceier.ifrance.com/lib/partenze-per.htm partenze per milano] [http://akmokanzen.ifrance.com/images/small/acer-n.htm acer n50 premium] [http://singaporepets.ifrance.com/library/html/adsl-pppoa.htm adsl pppoa modem] [http://jdawsona.ifrance.com/data/voce-me/ voce me apareceu video] [http://toofarfrommaybe.ifrance.com/view/new/hotel-lungomare/ hotel lungomare a cattolica] [http://highbulp.ifrance.com/images/small/audi-sicilia/ audi sicilia] [http://oiyaoi.ifrance.com/images/small/wt/ wt 615] [http://tat-ooin.ifrance.com/resources/articles/hello-evanescence.htm hello evanescence] [http://voltia.ifrance.com/content/view/www-latin.htm www latin chat] [http://voltia.ifrance.com/content/view/hdv-sony.htm hdv sony] [http://jdawsona.ifrance.com/data/batamn/ batamn 3] [http://broadwaylili.ifrance.com/content/view/eusebio-robledo.htm eusebio robledo colombiano] [http://demurediablo.ifrance.com/library/html/carlo-scarpa/ carlo scarpa] [http://demurediablo.ifrance.com/library/html/le-/ le  puni] [http://lovereceier.ifrance.com/lib/un-poliziotto.htm un poliziotto scomodo] [http://girlwho-is.ifrance.com/img/styles/acer-aspire.htm acer aspire rc810] [http://speedofsoun.ifrance.com/text/directory/codici-sblocco.htm codici sblocco autoradio] [http://broadwaylili.ifrance.com/content/view/acchiappa-te.htm acchiappa te] [http://speedofsoun.ifrance.com/text/directory/la-vedova.htm la vedova non veste di nero] [http://toofarfrommaybe.ifrance.com/view/new/marco-masini/ marco masini il giorno di natale] [http://singaporepets.ifrance.com/library/html/megan-calendario.htm megan calendario 2001] [http://singaporepets.ifrance.com/library/html/alzara.htm alzara] [http://akmokanzen.ifrance.com/images/small/dvd-r.htm dvd r vergini verbatim] [http://highbulp.ifrance.com/images/small/lamante-del/ lamante del re] [http://tat-ooin.ifrance.com/resources/articles/ludwig-van.htm ludwig van beethoven. symphony no. 9 corale] [http://lovereceier.ifrance.com/lib/campeggio-ischia.htm campeggio ischia] [http://highbulp.ifrance.com/images/small/sborrate-trans/ sborrate trans] [http://girlwho-is.ifra
+
{{Infobox See Also String}}
 +
==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===
 +
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) {
 +
    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]/}"