Strip control codes and extended characters from a string: Difference between revisions
(→{{header|J}}: Add J) |
(→{{header|J}}: Added PureBasic) |
||
Line 28: | Line 28: | ||
[[Category:String manipulation]] |
[[Category:String manipulation]] |
||
=={{header|PureBasic}}== |
|||
<lang PureBasic>Procedure.s stripControlCodes(source.s) |
|||
Protected i, *ptrChar.Character, length = Len(source), result.s |
|||
*ptrChar = @source |
|||
For i = 1 To length |
|||
If *ptrChar\c > 31 |
|||
result + Chr(*ptrChar\c) |
|||
EndIf |
|||
*ptrChar + SizeOf(Character) |
|||
Next |
|||
ProcedureReturn result |
|||
EndProcedure |
|||
Procedure.s stripControlExtCodes(source.s) |
|||
Protected i, *ptrChar.Character, length = Len(source), result.s |
|||
*ptrChar = @source |
|||
For i = 1 To length |
|||
If *ptrChar\c > 31 And *ptrChar\c < 128 |
|||
result + Chr(*ptrChar\c) |
|||
EndIf |
|||
*ptrChar + SizeOf(Character) |
|||
Next |
|||
ProcedureReturn result |
|||
EndProcedure |
|||
If OpenConsole() |
|||
;create sample string |
|||
Define i, s.s |
|||
For i = 1 To 80 |
|||
s + Chr(Random(254) + 1) ;include character values from 1 to 255 |
|||
Next |
|||
PrintN(stripControlCodes(s)) ;string without control codes |
|||
PrintN("---------") |
|||
PrintN(stripControlExtCodes(s)) ;string without control codes or extended chars |
|||
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input() |
|||
CloseConsole() |
|||
EndIf</lang> |
|||
Sample output: |
|||
<pre>»╫=┐C─≡G(═ç╤â√╝÷╔¬ÿ▌x è4∞|)ï└⌐ƒ9²òτ┌ºáj)▓<~-vPÿφQ╨ù¿╖îFh"[ü╗dÉ₧q#óé├p╫■ |
|||
--------- |
|||
=CG(x 4|)9j)<~-vPQFh"[dq#p</pre> |
Revision as of 04:24, 6 June 2011
The task is to strip control codes and extended characters from a string. The solution should demonstrate how to achieve each of the following results:
- a string with control codes stripped (but extended characters not stripped)
- a string with control codes and extended characters stripped
Ascii
In ascii, the control codes have decimal codes 0 through to 31 and the extended characters have decimal codes greater than 127. On an ascii based system, if the control codes and the extended characters are stripped, the resultant string would have all of its characters within the range of 32 to 127 decimal on the ascii table.
Non Ascii Based Systems
On a non ascii based system, we consider characters that do not have a corresponding glyph on the ascii table (within the ascii range of 32 to 127 decimal) to be an extended character for the purpose of this task.
J
Solution: <lang j>stripControlCodes=: #~ 31 < a.&i. stripControlExtCodes=: #~ (31&< *. 128&>)@(a.&i.)</lang> Usage: <lang j> mystring=: a. {~ 256?256 NB. ascii chars 0-255 in random order
#mystring NB. length of string
256
#stripControlCodes mystring NB. length of string without control codes
224
#stripControlExtCodes mystring NB. length of string without control codes or extended chars
96</lang>
PureBasic
<lang PureBasic>Procedure.s stripControlCodes(source.s)
Protected i, *ptrChar.Character, length = Len(source), result.s *ptrChar = @source For i = 1 To length If *ptrChar\c > 31 result + Chr(*ptrChar\c) EndIf *ptrChar + SizeOf(Character) Next ProcedureReturn result
EndProcedure
Procedure.s stripControlExtCodes(source.s)
Protected i, *ptrChar.Character, length = Len(source), result.s *ptrChar = @source For i = 1 To length If *ptrChar\c > 31 And *ptrChar\c < 128 result + Chr(*ptrChar\c) EndIf *ptrChar + SizeOf(Character) Next ProcedureReturn result
EndProcedure
If OpenConsole()
;create sample string Define i, s.s For i = 1 To 80 s + Chr(Random(254) + 1) ;include character values from 1 to 255 Next
PrintN(stripControlCodes(s)) ;string without control codes PrintN("---------") PrintN(stripControlExtCodes(s)) ;string without control codes or extended chars Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input() CloseConsole()
EndIf</lang> Sample output:
»╫=┐C─≡G(═ç╤â√╝÷╔¬ÿ▌x è4∞|)ï└⌐ƒ9²òτ┌ºáj)▓<~-vPÿφQ╨ù¿╖îFh"[ü╗dÉ₧q#óé├p╫■ --------- =CG(x 4|)9j)<~-vPQFh"[dq#p