Solved

Working with special characters in strings in VB.NET

Posted on 2012-04-13
9
335 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 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

18 Experts available now in Live!

Get 1:1 Help Now