Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 362
  • Last Modified:

Working with special characters in strings in VB.NET

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
Mike Miller
Asked:
Mike Miller
  • 4
  • 4
1 Solution
 
nepaluzCommented:
Do you not get any error(s) when you declare this?
Dim myStringRem As String = "mwmiller78,.75"&1"2400"

Open in new window

0
 
Mike MillerSoftware EngineerAuthor Commented:
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
 
nepaluzCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Mike MillerSoftware EngineerAuthor Commented:
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
 
wdosanjosCommented:
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
 
nepaluzCommented:
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
 
Mike MillerSoftware EngineerAuthor Commented:
I got it using...

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

Probably not the best practice but...



Thanks all
0
 
nepaluzCommented:
Hmm! time well spent then.
0
 
Mike MillerSoftware EngineerAuthor Commented:
Figured it out
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now