Character replacement for Control Sequence Introducer Character in VB? Chr(155) - vb.net

I have tried all day to replace this character I have in a string which has characters in it... and after googling, I found is called a "Control Sequence Introducer".
It looks like the hex code is 9B and the ASCII code is 155. (I think, from what I've read).
The string comes from a file which I read in, I have some null characters to replace, which is working fine, but just after that I've been working to remove this wierd character.
In notepad++ when I do show all symbols, it looks like this:
I tried the following:
strLine = strLine.Replace(Chr(155), " ")
strLine = Replace(strLine , "9B", " ")
strLine = Replace(strLine , "&#x9B", " ")
strLine = Replace(strLine , Chr(155), " ")
strLine= Regex.Replace(strLine, "\c#", " ")
strLine= Regex.Replace(strLine, "\c_", " ")
strLine= Regex.Replace(strLine, "\c", " ")
strLine= Regex.Replace(strLine, "\cA", " ")
strLine= Regex.Replace(strLine, "\cZ", " ")
I found a good section in wikipedia
Control Sequence Introducer Character ANSI Control Sequences
With everything going on in that, perhaps I have the wrong hex code?
Does anybody know how to replace this character? I have googled this for awhile now and it's elusive to me how to solve this.

I did find it finally, using regex and the hex code!
strLine = Regex.Replace(strLine, "\x9B", " ")

Related

Getting this error: "Conversion from string "" to type 'Double' is not valid." visual studio

Im trying to make a app for cafe. It has a simple interface coffee names with labels and quantity with numericupdown, receipt textbox and receipt button. I coded receipt button to show coffee name and quantity in receipt textbox like this:
If (espresso.Value > 0) Then receipt.AppendText("Espresso" + vbTab + vbTab + espresso.Value.ToString + Environment.NewLine)
that works fine but i want to add the price next to quantity of the coffee so i added these lines :
Dim espressoprice As Double
espressoprice = 3
Dim espressoquantity As Double = Convert.ToDouble(espresso.Value)
Dim espressototal As Double
espressototal = (espressoprice * espressoquantity)
(espresso.value is numericupdown value)
and changed the first codeline like this:
If (espresso.Value > 0) Then receipt.AppendText("Espresso" + vbTab + vbTab + espresso.Value.ToString + vbTab + espressototal + Environment.NewLine)
but i keep getting this error:
"Espresso 2 " "Conversion from string "" to type 'Double' is not valid."
What am i doing wrong please help.
The proper solution to this problem is to use the correct operator. You are trying to perform string concatenation but you are using the addition operator. This:
"Espresso" + vbTab + vbTab + espresso.Value.ToString + vbTab + espressototal + Environment.NewLine
is actually performing multiple additions. Addition maps to concatenation for two Strings but for numbers, addition is mathematical, NOT textual. In order to add a String and a numeric value, the system has to implicitly convert one of them to the other type. You are obviously assuming that the number will be converted to a String but it's actually the opposite that is happening, i.e. the system is trying to convert a String to a number and it is failing. This is why you should not rely on implicit conversions. If you used the concatenation operator, as you should when performing concatenation, then there's only one way it can go:
"Espresso" & vbTab & vbTab & espresso.Value.ToString & vbTab & espressototal & Environment.NewLine
Notice that, in this case, you don't have to explicitly convert the number to a String because the concatenation operator is defined for Strings and numeric values. Concatenation is a String operation so you know for a fact that everything that can be treated as a String, will be.
That said, there are better options anyway, e.g.
receipt.AppendText(String.Concat("Espresso", vbTab, vbTab, espresso.Value, vbTab, espressototal, Environment.NewLine)
In your line where you added expressototal you need to convert its value to a string in order to add it to other strings, that is, expressototal.ToString.

ms access form fields into a string vba

I have a form (say) Data, which contains text boxes A,B,C.
I wish to write an email based on the form data. In my email body, I want the following format:
A is : (actual value of A in text box in the form) (a newline)
B is : ((actual value of B in text box in the form) ( a newline)
C is : ((actual value of C in text box in the form).
I know I can access values by Forms!Data!A_value (assuming I named the box as A_value). I am not able to combine them into a string and add a newline too.
I have tried the following:
Dim body as String
body = "A is : & Forms!Data!A_value &" & "B is : & Forms!Data!B_value &" & "C is : & Forms!Data!C_value &"
It is because I read an & results to a new line somewhere.
However, when i do that, the whole thing is concatenated as written in the code and no values are obtained from the form field.
Please suggest options:
Thanks in advance
You probably want something like:
Dim body as String
body = "A is : " & Forms!Data!A_value & vbNewLine & _
"B is : " & Forms!Data!B_value & vbNewLine & _
"C is : " & Forms!Data!C_value
Note: the fact that I wrote that code on 3 lines, using line continuation characters, is nothing to do with the insertion of the new line characters in the output. It could have also been written as
body = "A is : " & Forms!Data!A_value & vbNewLine & "B is : " & Forms!Data!B_value & vbNewLine & "C is : " & Forms!Data!C_value
but I find that harder to read.
The ampersand is used to concatenate text "hello " & "there".
If you quote the ampersand it does nothing, just reproduces the ampersand "bits & bobs".
You can use the character vbCrLf (carriage return/linefeed) to add (concatenate) a linebreak, "time for " & vbCrLf & "a break".
Another trick you can use is to create a single string template with placeholders for the values, then use Replace statements to fill them in, like:
body = "A is: {A} & B is: {B} & C is: {C}"
body = Replace(body, "{A}", Forms!Data!A_value)
body = Replace(body, "{B}", Forms!Data!B_value)
body = Replace(body, "{C}", Forms!Data!C_value)
And break out to multiple lines, like:
body = "A is: {A}{CR}B is: {B}{CR}C is: {C}{CR}"
body = Replace(body, "{A}", Forms!Data!A_value)
body = Replace(body, "{B}", Forms!Data!B_value)
body = Replace(body, "{C}", Forms!Data!C_value)
body = Replace(body, "{CR}", vbCrLf)

VBA Replace() doesn't replace characters

This is odd. In the code below the replace() does not replace the : character. Why? How do I fix this? Even if I switch ":" with chr(58) it doesn't work.
Dim dispName as String
dispName = " 110531 Reļ¼šOur file 027-10.doc"
dispName = Replace(dispName, ":", " ")
msgbox dispName
I copied your code as it is to notepad++ and found that used both ':' characters are different.
Change both to the same and run the code

Left split, getting blank return.

Issue, where the character I am removing does not exist I get a blank string
Aim: To look for three characters in order and only get the characters to the left of the character I am looking for. However if the character does not exist then to do nothing.
Code:
Dim vleftString As String = File.Name
vleftString = Left(vleftString, InStr(vleftString, "-"))
vleftString = Left(vleftString, InStr(vleftString, "_"))
vleftString = Left(vleftString, InStr(vleftString, " "))
As a 'fix' I have done
Dim vleftString As String = File.Name
vleftString = Replace(vleftString, "-", " ")
vleftString = Replace(vleftString, "_", " ")
vleftString = Left(vleftString, InStr(vleftString, " "))
vleftString = Trim(vleftString)
Based on Left of a character in a string in vb.net
If File.Name is say 1_2.pdf it passes "-" and then works on line removing anything before "" (though not "" though I want it to)
When it hits the line for looking for anything left of space it then makes vleftString blank.
Since i'm not familiar (and avoid) the old VB functions here a .NET approach. I assume you want to remove the parts behind the separators "-", "_" and " ", then you can use this loop:
Dim fileName = "1_2.pdf".Trim() ' Trim used to show you the method, here nonsense
Dim name = Path.GetFileNameWithoutExtension(fileName).Trim()
For Each separator In {"-", "_", " "}
Dim index = name.IndexOf(separator)
If index >= 0 Then
name = name.Substring(0, index)
End If
Next
fileName = String.Format("{0}{1}", name, Path.GetExtension(fileName))
Result: "1.pdf"

What does "& _" mean in VB?

I'm copying some query statements from a legacy VB app to a C# app. I am not familiar with VB, although looking at it makes me want a VB (Victoria Bitter). I have come across queries constructed like this:
*SELECT dp_duckbill_accounts.platypus_no AS duckbill, t_accounts.name AS Name " & _
"FROM t_accounts INNER JOIN dp_duckbill_accounts ON t_accounts.account_no = dp_duckbill_accounts.account_no " & _
"ORDER BY dp_duckbill_accounts.platypus_no*
The "& _" give me pause. If it was just "&" I would think it corresponds to "+" in C# to concatenate strings. But what in the world is the point of the underscore? Note the ampersand and the underscore are separated by a space.
The underscore is the line continuation character. It allows the concatenation to include a different line. Like so:
x = "Hello " & "World"
x = "Hello " & _
"World"
'this won't compile (pre vb.net 2010, anyway)
x = "Hello " &
"World"
Line Continuation on MSDN
How to: Break and Combine Statements in Code (Visual Basic)
_ means continue the statement on the following line.
so ... & _ means continue concatenating the string on the following line.
text = "One line string"
text = "Two line " & _
"string"
That is just a line continuation character that lets you continue to the next line.
& - is used for string concatenation in same line.
example - sConcatenatedString = "First" & "Second"
& _ - is used For string concatenation in different lines.
example - sConcatenatedString = "First" &_
"Second"