473,378 Members | 1,619 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,378 software developers and data experts.

String.Replace and Objects

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
4 1574
"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
* "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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Roose | last post by:
How can I do a "".replace operation which tells me if anything was actually replaced? Right now I am doing something like: if searchTerm in text: text = text.replace( searchTerm, 'other' ) ...
1
by: Thomas | last post by:
It looks like the String.replace doesn't work in IE6.1. Anyone else has the same problem. I am using newest service package of IE and Win2K. Thanks
4
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
9
by: Crirus | last post by:
dim pp as string pp="{X=356, Y=256}{X=356, Y=311.2285}{X=311.2285, Y=356}{X=256, Y=356}{X=200.7715, Y=356}{X=156, Y=311.2285}{X=156, Y=256}{X=156, Y=200.7715}{X=200.7715, Y=156}{X=256,...
4
by: Craig Buchanan | last post by:
If I have a string variable, is there a way to get the Replace method to work on *its* contents, without having to dimenstion a second variable? Something like: Dim MyTest as String = "<hello/>"...
21
by: gary | last post by:
How would one make the ECMA-262 String.replace method work with a string literal? For example, if my string was "HELLO" how would I make it work in this instance. Please note my square...
8
by: Johny | last post by:
Let's suppose s='12345 4343 454' How can I replace the last '4' character? I tried string.replace(s,s,'r') where 'r' should replace the last '4'. But it doesn't work. Can anyone explain why?...
3
by: Sin Jeong-hun | last post by:
If I use something like this, string html = "<h1>C# is great</h1>"; Console.WriteLine(html.Replace("&lt;","<").Replace("&gt;",">")); Does this recreate new string objects two times, even though it...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.