473,322 Members | 1,232 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Replace any chars not in allowed list

I need an elegant way to remove any characters in a string if they are
not in an allowed char list. The part cleaning files of the
non-allowed characters will run as a service, so no forms here.

The list also needs to be editable by the end-user so I'll be
providing a form on which they can edit the allowed character list.

The end-user is non-technical so asking them to type a regular
expression is out.

Is there anything in the framework 2.0 to clean a string of unwanted
characters, or better, clean any characters not in an allow list?
Seems like there would be to clean SQL strings to prevent injection
attacks.
Feb 14 '08 #1
7 3774

string.Replace work fine for me:

string Description = row["Desc"].ToString();
Description = Description.Replace("\n", " | ");//Remove line feed
Description = Description.Replace("\r", " | ");//Remove line feed
char c=Convert.ToChar(19);
Description = Description.Replace(c, ":".ToCharArray()[0]);
schneider

<Grokwrote in message news:o5********************************@4ax.com...
>I need an elegant way to remove any characters in a string if they are
not in an allowed char list. The part cleaning files of the
non-allowed characters will run as a service, so no forms here.

The list also needs to be editable by the end-user so I'll be
providing a form on which they can edit the allowed character list.

The end-user is non-technical so asking them to type a regular
expression is out.

Is there anything in the framework 2.0 to clean a string of unwanted
characters, or better, clean any characters not in an allow list?
Seems like there would be to clean SQL strings to prevent injection
attacks.

Feb 14 '08 #2
Thanks but opposite of my needs. I am not looking for a way to
replace a known character, but to remove any characters that are NOT
in a given character list. Something like:

Function CleanAString(ByVal theDirtyString As String) As String
Dim allowedChars As String
allowedChars = GetAllowedCharsFromRegKey()
Return String.AwesomeStringCleaner(theDirtyString, allowedChars)
End Function

with "abcdef" on allowed list,
CleanAString("aa+bb,cc112233/dd\ee:ff")
returns "aabbccddeeff"
On Thu, 14 Feb 2008 14:02:19 -0600, "schneider"
<es****************@starkinvestments.comwrote:
>
string.Replace work fine for me:

string Description = row["Desc"].ToString();
Description = Description.Replace("\n", " | ");//Remove line feed
Description = Description.Replace("\r", " | ");//Remove line feed
char c=Convert.ToChar(19);
Description = Description.Replace(c, ":".ToCharArray()[0]);
schneider

<Grokwrote in message news:o5********************************@4ax.com...
>>I need an elegant way to remove any characters in a string if they are
not in an allowed char list. The part cleaning files of the
non-allowed characters will run as a service, so no forms here.

The list also needs to be editable by the end-user so I'll be
providing a form on which they can edit the allowed character list.

The end-user is non-technical so asking them to type a regular
expression is out.

Is there anything in the framework 2.0 to clean a string of unwanted
characters, or better, clean any characters not in an allow list?
Seems like there would be to clean SQL strings to prevent injection
attacks.
Feb 14 '08 #3
Dim tempString As String = theDirtyString

For Each c as Char In theDirtyString
If Not allowedChars.Contains(c.ToString) Then
tempString = tempString.Replace(c.ToString, String.Empty)
End If
Next

Return tempString

Something like that...?

"Grok" <gr**@valhallalegends.comwrote in message
news:ks********************************@4ax.com...
Thanks but opposite of my needs. I am not looking for a way to
replace a known character, but to remove any characters that are NOT
in a given character list. Something like:

Function CleanAString(ByVal theDirtyString As String) As String
Dim allowedChars As String
allowedChars = GetAllowedCharsFromRegKey()
Return String.AwesomeStringCleaner(theDirtyString, allowedChars)
End Function

with "abcdef" on allowed list,
CleanAString("aa+bb,cc112233/dd\ee:ff")
returns "aabbccddeeff"
On Thu, 14 Feb 2008 14:02:19 -0600, "schneider"
<es****************@starkinvestments.comwrote:
>>
string.Replace work fine for me:

string Description = row["Desc"].ToString();
Description = Description.Replace("\n", " | ");//Remove line feed
Description = Description.Replace("\r", " | ");//Remove line feed
char c=Convert.ToChar(19);
Description = Description.Replace(c, ":".ToCharArray()[0]);
schneider

<Grokwrote in message news:o5********************************@4ax.com...
>>>I need an elegant way to remove any characters in a string if they are
not in an allowed char list. The part cleaning files of the
non-allowed characters will run as a service, so no forms here.

The list also needs to be editable by the end-user so I'll be
providing a form on which they can edit the allowed character list.

The end-user is non-technical so asking them to type a regular
expression is out.

Is there anything in the framework 2.0 to clean a string of unwanted
characters, or better, clean any characters not in an allow list?
Seems like there would be to clean SQL strings to prevent injection
attacks.

Feb 15 '08 #4
Clever and perfect!

On Thu, 14 Feb 2008 19:10:09 -0600, "Alex Clark" <al**@microsoft.com>
wrote:
>Dim tempString As String = theDirtyString

For Each c as Char In theDirtyString
If Not allowedChars.Contains(c.ToString) Then
tempString = tempString.Replace(c.ToString, String.Empty)
End If
Next

Return tempString

Something like that...?

"Grok" <gr**@valhallalegends.comwrote in message
news:ks********************************@4ax.com.. .
>Thanks but opposite of my needs. I am not looking for a way to
replace a known character, but to remove any characters that are NOT
in a given character list. Something like:

Function CleanAString(ByVal theDirtyString As String) As String
Dim allowedChars As String
allowedChars = GetAllowedCharsFromRegKey()
Return String.AwesomeStringCleaner(theDirtyString, allowedChars)
End Function

with "abcdef" on allowed list,
CleanAString("aa+bb,cc112233/dd\ee:ff")
returns "aabbccddeeff"
On Thu, 14 Feb 2008 14:02:19 -0600, "schneider"
<es****************@starkinvestments.comwrote:
>>>
string.Replace work fine for me:

string Description = row["Desc"].ToString();
Description = Description.Replace("\n", " | ");//Remove line feed
Description = Description.Replace("\r", " | ");//Remove line feed
char c=Convert.ToChar(19);
Description = Description.Replace(c, ":".ToCharArray()[0]);
schneider

<Grokwrote in message news:o5********************************@4ax.com...
I need an elegant way to remove any characters in a string if they are
not in an allowed char list. The part cleaning files of the
non-allowed characters will run as a service, so no forms here.

The list also needs to be editable by the end-user so I'll be
providing a form on which they can edit the allowed character list.

The end-user is non-technical so asking them to type a regular
expression is out.

Is there anything in the framework 2.0 to clean a string of unwanted
characters, or better, clean any characters not in an allow list?
Seems like there would be to clean SQL strings to prevent injection
attacks.
Feb 15 '08 #5
Grok wrote:
Is there anything in the framework 2.0 to clean a string of unwanted
characters, or better, clean any characters not in an allow list?
Seems like there would be to clean SQL strings to prevent injection
attacks.
If SQL is your objective then consider using parameterised queries instead:
they are automatically injection-proof.

Andrew
Feb 15 '08 #6
Grok wrote:
Clever and perfect!

On Thu, 14 Feb 2008 19:10:09 -0600, "Alex Clark" <al**@microsoft.com>
wrote:
>Dim tempString As String = theDirtyString

For Each c as Char In theDirtyString
If Not allowedChars.Contains(c.ToString) Then
tempString = tempString.Replace(c.ToString, String.Empty)
End If
Next

Return tempString
A little more efficent, maybe?

Dim SB As New System.Text.StringBuilder

For Each c As Char In theDirtyString
If allowedChars.IndexOf(c) 0 Then
SB.Append(c)
End If
Next c

Return SB.ToString

Feb 15 '08 #7
A little more efficent, maybe?
>
Dim SB As New System.Text.StringBuilder

For Each c As Char In theDirtyString
If allowedChars.IndexOf(c) 0 Then
SB.Append(c)
End If
Next c

Return SB.ToString
Memory-wise yes, StringBuilder will be far easier on the working set (and
CPU) than just using System.String (and optimisation was left up to the OP
as far as I was concerned). However, your solution is technically
incorrect. IndexOf is 0 based in .NET, therefore you should have a <0
rather than 0.

Happy to help ;-P

Feb 15 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Frances Del Rio | last post by:
Pls, what are all illegal chars in e-mail addreses? specif. I'd like to know if apostrophe is allowed, but would be handy to find a list of all illegal chars for e-mail addresses.. searched...
4
by: GregMa | last post by:
Does anyone have a good regex expression to replace any invalid filename characters in a string? Those characters are: /, \, :, *, ?, ", <, >, | I have it right now with string.replace for each...
7
by: gar | last post by:
Hi, I need to replace all the double quotes (") in a textbox with single quotes ('). I used this code text= Replace(text, """", "'" This works fine (for normal double quotes).The problem...
4
by: jgabbai | last post by:
Hi, What is the best way to white list a set of allowable characters using regex or replace? I understand it is safer to whitelist than to blacklist, but am not sure how to go about it. Many...
5
by: Michele Petrazzo | last post by:
Hi, a lot of times I need to replace more than one char into a string, so I have to do something like value = "test" chars = "e" for c in chars: value = value.replace(c, "") A solution...
5
by: Martin Kulas | last post by:
Hello! How do I replace a single character in a string at a given position? From programming languages like C I expect something like that: Traceback (most recent call last): File...
1
by: | last post by:
dim br as BinaryReader(stream) dim buf(5000) as byte br.Read(buf,0,5000) closeit.. I have read a binary file into a buffer. This buffer contains many different chars as well as a few null chars....
13
by: Hongyu | last post by:
Hi, I have a datetime char string returned from ctime_r, and it is in the format like ""Wed Jun 30 21:49:08 1993\n\0", which has 26 chars including the last terminate char '\0', and i would...
11
by: bcurtu | last post by:
Hi, I have a BIIIIIG problem with the next query: cursor.execute(""" SELECT titem.object_id, titem.tag_id FROM tagging_taggeditem titem WHERE titem.object_id IN (%s) """,( eid_list))
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.