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
Solved

Working with special characters in strings in VB.NET

Posted on 2012-04-13
9
342 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Import Data from Multiple Text Files in Excel 12 58
Call Controller Action Method from ASPX 2 22
VB.Net. Reading xml value 6 29
VB.NET Textbox input validation 4 37
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

792 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