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

From CodeCodex

(Python)
(add Ruby)
Line 1: Line 1:
 
{{Infobox See Also String}}
 
{{Infobox See Also String}}
 
==Implementations==
 
==Implementations==
===Java===
 
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]","");
 
}
 
 
 
===C===
 
===C===
 
if buff's content is: "15+41-2Hel54lo **1212 Wor2ld! Ho5w Are 6996 Yo7u?" then
 
if buff's content is: "15+41-2Hel54lo **1212 Wor2ld! Ho5w Are 6996 Yo7u?" then
Line 26: Line 20:
 
}
 
}
 
</HIGHLIGHTSYNTAX>
 
</HIGHLIGHTSYNTAX>
 +
 +
===Java===
 +
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===
 
===JavaScript===
Line 66: Line 66:
 
print re.sub("[^A-Za-z]", "", s)
 
print re.sub("[^A-Za-z]", "", s)
 
</HIGHLIGHTSYNTAX>
 
</HIGHLIGHTSYNTAX>
 +
 +
===Ruby===
 +
<pre>
 +
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 ===
 
=== Tcl ===
Line 119: Line 127:
 
</pre>
 
</pre>
  
[[Category:Java]]
+
[[Category:String]]
 
[[Category:C]]
 
[[Category:C]]
 +
[[Category:Java]]
 
[[Category:JavaScript]]
 
[[Category:JavaScript]]
 
[[Category:Objective Caml]]
 
[[Category:Objective Caml]]
Line 126: Line 135:
 
[[Category:PHP]]
 
[[Category:PHP]]
 
[[Category:Python]]
 
[[Category:Python]]
 +
[[Category:Ruby]]
 
[[Category:Tcl]]
 
[[Category:Tcl]]
[[Category:String]]
 
 
[[Category:Visual Basic]]
 
[[Category:Visual Basic]]
 
[[Category:WinBatch]]
 
[[Category:WinBatch]]
 
[[Category:Zsh]]
 
[[Category:Zsh]]

Revision as of 13:47, 3 July 2010

Related content:

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" <HIGHLIGHTSYNTAX language="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++; } } </HIGHLIGHTSYNTAX>

Java

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

<HIGHLIGHTSYNTAX language="javascript"> var s = "how19 a*re 254y**ou?";

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

OCaml

# 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

<HIGHLIGHTSYNTAX language="perl">

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

</HIGHLIGHTSYNTAX>

PHP

<HIGHLIGHTSYNTAX language="php3"> $result = preg_replace('/[^A-Za-z]/', , $string); </HIGHLIGHTSYNTAX>

Python

<HIGHLIGHTSYNTAX language="python"> s = "hello world! how are you? 0"

  1. Short version

print filter(lambda c: c.isalpha(), s)

  1. 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)

  1. Using regular expressions

print re.sub("[^A-Za-z]", "", s) </HIGHLIGHTSYNTAX>

Ruby

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

Tcl

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

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

; 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

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