By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,041 Members | 1,046 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,041 IT Pros & Developers. It's quick & easy.

String.Replace and Objects

P: n/a
I have an object with several string elements that I would like to check for
invalid characters in the properties of each element. Can I use
string.replace to do that or is there a better alternative to this?
Example:

Property orgname() As String
Get
orgname = m_orgname
End Get
Set(ByVal Value As String)
If Value = "" Then Throw New ArgumentException("OrgName cannot be
null or blank")
value.Replace("'", "&&")
value.Replace(";", "&&")
value.replace(",", "&&")
m_orgname = Value
End Set
End Property

Is there an easier way to do this/a way to do it with fewer repetative lines
of code?

Thanks,
Derek
--
Derek Martin
593074
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Derek Martin" <dm*****@DONTSPAMMEokstateDOT.edu> wrote in
news:Oz*************@tk2msftngp13.phx.gbl:
Is there an easier way to do this/a way to do it with fewer repetative
lines of code?


Try using Regular Expressions.

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 21 '05 #2

P: n/a
* "Derek Martin" <dm*****@DONTSPAMMEokstateDOT.edu> scripsit:
I have an object with several string elements that I would like to check for
invalid characters in the properties of each element. Can I use
string.replace to do that or is there a better alternative to this?
Example:

Property orgname() As String
Get
orgname = m_orgname
End Get
Set(ByVal Value As String)
If Value = "" Then Throw New ArgumentException("OrgName cannot be
null or blank")
value.Replace("'", "&&")
value.Replace(";", "&&")
value.replace(",", "&&")


Use 'value = value.Replace(...)'. 'Replace' is a function, not a sub.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #3

P: n/a
Derek,
As Herfried suggests, String.Replace is a function you need to assign the
value returned to a variable.

If I have more then 5 to 10 replaces I would consider using a StringBuilder
instead of String.Replace.

Something like:
If Value = "" Then Throw New ArgumentException("OrgName cannot be
null or blank") Dim sb As New StringBuilder(value, value.Length * 2)
sb.Replace("'", "&&")
sb.Replace(";", "&&")
sb.replace(",", "&&")
m_orgname = sb.ToString

If you are replacing all the matched string with the same value (as it
appears you are) I would also consider using a RegEx.
If Value = "" Then Throw New ArgumentException("OrgName cannot be
null or blank") Dim re As New RegEx("'|;|,")
m_orgname = re.Replace(value, "&&"

If I used the RegEx approach I would consider making the RegEx itself a
shared or static variable and passing the RegexOptions.Compiled option to
the RegEx constructor.

Static re As New RegEx("'|;|,", RegexOptions.Compiled)
m_orgname = re.Replace(value, "&&"

Double check the reg ex pattern, I did not test it.

Hope this helps
Jay

"Derek Martin" <dm*****@DONTSPAMMEokstateDOT.edu> wrote in message
news:Oz*************@tk2msftngp13.phx.gbl... I have an object with several string elements that I would like to check for invalid characters in the properties of each element. Can I use
string.replace to do that or is there a better alternative to this?
Example:

Property orgname() As String
Get
orgname = m_orgname
End Get
Set(ByVal Value As String)
If Value = "" Then Throw New ArgumentException("OrgName cannot be
null or blank")
value.Replace("'", "&&")
value.Replace(";", "&&")
value.replace(",", "&&")
m_orgname = Value
End Set
End Property

Is there an easier way to do this/a way to do it with fewer repetative lines of code?

Thanks,
Derek
--
Derek Martin
593074

Nov 21 '05 #4

P: n/a
Wonderful, thank you all for your help!

Derek

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:Of**************@TK2MSFTNGP12.phx.gbl...
Derek,
As Herfried suggests, String.Replace is a function you need to assign the
value returned to a variable.

If I have more then 5 to 10 replaces I would consider using a
StringBuilder
instead of String.Replace.

Something like:
If Value = "" Then Throw New ArgumentException("OrgName cannot be
null or blank")

Dim sb As New StringBuilder(value, value.Length * 2)
sb.Replace("'", "&&")
sb.Replace(";", "&&")
sb.replace(",", "&&")
m_orgname = sb.ToString

If you are replacing all the matched string with the same value (as it
appears you are) I would also consider using a RegEx.
If Value = "" Then Throw New ArgumentException("OrgName cannot be
null or blank")

Dim re As New RegEx("'|;|,")
m_orgname = re.Replace(value, "&&"

If I used the RegEx approach I would consider making the RegEx itself a
shared or static variable and passing the RegexOptions.Compiled option to
the RegEx constructor.

Static re As New RegEx("'|;|,", RegexOptions.Compiled)
m_orgname = re.Replace(value, "&&"

Double check the reg ex pattern, I did not test it.

Hope this helps
Jay

"Derek Martin" <dm*****@DONTSPAMMEokstateDOT.edu> wrote in message
news:Oz*************@tk2msftngp13.phx.gbl...
I have an object with several string elements that I would like to check

for
invalid characters in the properties of each element. Can I use
string.replace to do that or is there a better alternative to this?
Example:

Property orgname() As String
Get
orgname = m_orgname
End Get
Set(ByVal Value As String)
If Value = "" Then Throw New ArgumentException("OrgName cannot be
null or blank")
value.Replace("'", "&&")
value.Replace(";", "&&")
value.replace(",", "&&")
m_orgname = Value
End Set
End Property

Is there an easier way to do this/a way to do it with fewer repetative

lines
of code?

Thanks,
Derek
--
Derek Martin
593074


Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.