Solved

Working with special characters in strings in VB.NET

Posted on 2012-04-13
9
338 Views
Last Modified: 2012-04-18
I'm comparing 2 strings by using

Public Function Whatevs(ByVal myString As String)
Dim myStringRem As String = "mwmiller78,.75"&1"2400"
If myStringRem.Contains(myString.Trim) then
'whatever
End If
return myString
End Function

Open in new window


I'm sending the following string to the function
mwmiller78,.75"&1"2400

When I step through the code though, the value is set as
mwmiller78,.75\"&1\"2400

How do I account for the \ being thrown in there so that I can get an accurate comparison?

TIA
0
Comment
Question by:mwmiller78
  • 4
  • 4
9 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 37844324
Do you not get any error(s) when you declare this?
Dim myStringRem As String = "mwmiller78,.75"&1"2400"

Open in new window

0
 

Author Comment

by:mwmiller78
ID: 37844369
Probably, I was trying to simplify my example just so readers would get an idea of what I'm talking about. The string actually comes from a text file i'm looping through line by line. When it returns the string I'm looking for it does not return it as the value is in the text file. It's returned how I displayed it above.
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 37844410
Over-simplification can lead to the obfuscation of the underlying issue, resulting in failure to provide working code. Would you mind to re-phrase / re-state the question?
0
 

Author Comment

by:mwmiller78
ID: 37844474
Sure...

I'm comparing 2 strings by using

Public Function Whatevs(ByVal myString As String)
Dim myStringRem As String
Dim objReader As New System.IO.StreamReader(C:\myFile2.txt)

Do While objReader.Peek() <> -1
myStringRem = objReader.ReadLine()
Loop

If myStringRem.Contains(myString.Trim) then
'whatever
End If
return myString
End Function

Open in new window


in which the file myFile2.txt only has one line that reads:
mwmiller78,.75"&1"2400

I'm sending a value to the function using:

Dim strTemp As String = ""
Dim objReader As New System.IO.StreamReader(C:\myFile.txt)

Do While objReader.Peek() <> -1
strTemp = objReader.ReadLine()
Loop

strTemp = Whatevs(strTemp)

Open in new window

in which there is only one line in myFile.txt. That line reads:
mwmiller78,.75"&1"2400

When I step through the code though, the value of myString is set as
mwmiller78,.75\"&1\"2400

How do I account for the \ being thrown in there so that I can get an accurate comparison?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 23

Expert Comment

by:wdosanjos
ID: 37844532
I believe mwmiller78,.75\"&1\"2400 is just the way VS displays the value in debug mode, but the actual value does not contain the \ chars.  Try outputting the variable to the console to verify Console.WriteLine(strTemp).
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 37844557
It probably has something to do with the encoding the  streamreader is deaulting to, though I have to say I can not replicate your results. I tried with slightly different coding and get:
MyResusing this code
Dim myStringRem As String
Using xRead As New StreamReader("C:\myFile2.txt")
    While Not xRead.EndOfStream
        myStringRem = xRead.ReadLine()
    End While
End Using

Open in new window

0
 

Accepted Solution

by:
mwmiller78 earned 0 total points
ID: 37844590
I got it using...

Replace("\" & ControlChars.Quote, ControlChars.Quote)

Probably not the best practice but...



Thanks all
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 37844939
Hmm! time well spent then.
0
 

Author Closing Comment

by:mwmiller78
ID: 37859742
Figured it out
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now