VB.net/SQL dealing with apostrophes in strings

Hi,
How Do I find the string has an apostrophe, single quote in the string.
I have a function
 Public Function ReplaceSingleQuote(ByRef val As String)
        '//***This function replaces the single quote to double quote***\\
        If IsNothing(val) = False OrElse val <> String.Empty Then
            val = val.Replace("'", "''")
            val = val.TrimStart
        End If
        Return val
    End Function

Open in new window

But, the problem here is that if use for multiple times for the same string it inserts too many single quotes.
Is there any way it just detects an single apostrophe and then replaces it with '' for sql compatibility.
Thanks
RIASAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

pls try
 Public Function ReplaceSingleQuote(ByRef val As String)
        '//***This function replaces the single quote to double quote***\\
        If IsNothing(val) = False OrElse val <> String.Empty Then
            val = val.Replace("''", "'")
            val = val.Replace("'", "''")
            val = val.TrimStart
        End If
        Return val
    End Function

Open in new window

Regards
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RIASAuthor Commented:
Thanks and brb
0
Rgonzo1971Commented:
Or you could use regex
Imports System.Text.RegularExpressions
'
      Dim pattern As String = "(')(?:[^'])"
      Dim replacement As String = "$1$1"
      Dim rgx As New Regex(pattern)
      val = rgx.Replace(val, replacement)

Open in new window

REgards
0
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

RIASAuthor Commented:
Thanks , The problem here is that its gets saved as   '' in sql database .
Is there any way it inserts only one quote
0
Rgonzo1971Commented:
Not sure what the problem is
0
RIASAuthor Commented:
Also if the string is
str = Count''''''''''''''''''''''''''''ry

Is there a way to return
str = Count''ry

Thanks
0
Rgonzo1971Commented:
then try
this regex
Imports System.Text.RegularExpressions
'
      Dim pattern As String = "('')(')+"
      Dim replacement As String = "$1"
      Dim rgx As New Regex(pattern)
      val = rgx.Replace(val, replacement)
      val = val.Replace("'", "''")

Open in new window

0
RIASAuthor Commented:
Thanks! Will try and brb.
0
RIASAuthor Commented:
I think your first solution is better.

Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.