Solved

Working with special characters in strings in VB.NET

Posted on 2012-04-13
9
344 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:Mike Miller
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:Mike Miller
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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

Author Comment

by:Mike Miller
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
 
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:
Mike Miller 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:Mike Miller
ID: 37859742
Figured it out
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 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