replacing chars

dgage

Member
Joined
Jul 23, 2007
Messages
14
Programming Experience
10+
I would like to take a string, and determine if there are any non-printable characters in it, like less than hex 20 for example, and if so, either drop them, or replace them with something printable.
I am having a heck of a time with this. Can anyone help ?
Thanks -

Doug
 
you can use the string's .Replace() method for this, just do one for each char you want replaced

for example:
myString = myString.Replace(Environment.NewLine, "") 'This replaces a line carriage return with nothing
 
what i would like to do is use a for loop for th length of the string and test the hex value of each character. if it is greater than a certain hex value or less than a certain hex value, then replace or remove.

I tried what you suggested ( used 'P' for a test) , but could not get that to work either -

Dim strRescanData As String

strRescanData = ("Problem=" & m_dtRescans.Rows(m_rowPosition)("Exception Notes") & "")

strRescanData.Replace("P", "X")


what am I doing wrong ? Sorry, but I am new to vb
 
whoops, forgot the return value. I have your example working now, thanks. I would still like to know how to do it by testing for < or > a given hex value
 
Try this (using the ASCII value, not Hex):

Dim mystr As String
mystr = TextBox1.Text 'or whatever the source of your string
For Each ch As Char In mystr
If Asc(ch) < 32 Or Asc(ch) > 127 Then
MessageBox.Show("Non-printable character")
End If
Next
 
Last edited:
Thank you so much for both replies. I have a use for each method. A question when using the .replace method -

It seems to work only on the last time I use it. For example,

strRescanData = ("Problem=" & m_dtRescans.Rows(m_rowPosition)("Exception Notes") & "")

Dim txtStr As String = strRescanData.ToCharArray

x = txtStr.Replace("a", "x")

x = txtStr.Replace(vbLf, "x")

only replaces the vbLF's, not the "a" characters.
can you provide any insight ?
 
Replace is a function, functions return the result of what they do. For your first call the result is assigned to "x" variable. For your second call you are using the unmodified "txtStr" variable and returning the result into same variable "x". The value assignment of a variable is the last one done, so your first call doesn't have any effect at all because you erased that result when you assigned a new value to the variable in next call. You really need to read your first VB.Net book, actually any programming language book will have taught you these things.
 
OMG ! I cannot believe how stupid I am. Thanks for pointing out the obvious to me. Fingers are moving across keys before the mind is in gear...sheesh.
You guys are great. Sorry for the idiotic post..

- Doug
 
also note, you can "stack" the .Replace method, for example:
Dim txtStr As String = strRescanData.Replace("a", "x").Replace(vbLf, "x")
 
The way I use it most is for booleans, displaying 'Yes' instead of 'True' like so:

Dim YesNo As String = BooleanVariable.ToString.ToLower.Replace("true", "Yes").Replace("false", "No")

I do this for reports, in code I use the boolean variable and discard the string representation (usually I don't even store the "Yes" or "No" in a variable)
 
Depending on how many replaces you're doing, RegEx might be a much better choice here, and much faster and less CPU/memory intensive. And only really requires one line of code. :eek:)
 
Back
Top