473,796 Members | 2,669 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ArgumentExcepti on("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 1595
"Derek Martin" <dm*****@DONTSP AMMEokstateDOT. edu> wrote in
news:Oz******** *****@tk2msftng p13.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********@rog ers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 21 '05 #2
* "Derek Martin" <dm*****@DONTSP AMMEokstateDOT. 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 ArgumentExcepti on("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 ArgumentExcepti on("OrgName cannot be
null or blank") Dim sb As New StringBuilder(v alue, 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 ArgumentExcepti on("OrgName cannot be
null or blank") Dim re As New RegEx("'|;|,")
m_orgname = re.Replace(valu e, "&&"

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

Static re As New RegEx("'|;|,", RegexOptions.Co mpiled)
m_orgname = re.Replace(valu e, "&&"

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

Hope this helps
Jay

"Derek Martin" <dm*****@DONTSP AMMEokstateDOT. edu> wrote in message
news:Oz******** *****@tk2msftng p13.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 ArgumentExcepti on("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******** ******@TK2MSFTN GP12.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 ArgumentExcepti on("OrgName cannot be
null or blank")

Dim sb As New StringBuilder(v alue, 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 ArgumentExcepti on("OrgName cannot be
null or blank")

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

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

Static re As New RegEx("'|;|,", RegexOptions.Co mpiled)
m_orgname = re.Replace(valu e, "&&"

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

Hope this helps
Jay

"Derek Martin" <dm*****@DONTSP AMMEokstateDOT. edu> wrote in message
news:Oz******** *****@tk2msftng p13.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 ArgumentExcepti on("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
2529
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' ) But to me this seems inefficient since the 'in' operation will search through the whole string, and then the 'replace' will as well.
1
16348
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
62116
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 supposed to write this function? String.replace(/</g,'&lt;');
32
14903
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 ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
9
2157
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, Y=156}{X=311.2285, Y=156}{X=356, Y=200.7715}{X=356, Y=256}{X=200, Y=150}{X=200, Y=177.6142}{X=177.6142, Y=200}{X=150, Y=200}{X=122.3858, Y=200}{X=100, Y=177.6142}{X=100, Y=150}{X=100, Y=122.3858}{X=122.3858, Y=100}{X=150, Y=100}{X=177.6142, Y=100}{X=200,...
4
1154
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/>" MyTest.Replace("<", "&lt;") MyTest.Replace(">", "&gt;") 'MyTest now equals "&lt;hello&gt;"
21
3413
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 brackets are not regular expression syntax. Thanks,
8
80300
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? Thanks
3
1967
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 has nothing to replace, and it is OK to return the same string object? I know, regular expression is a more delicate way to do this, but it always is kind of too complicated to me.
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9525
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10452
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10221
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10169
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.